question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PRIMARY KEY violation

See original GitHub issue

Describe your issue

I have a system that creates a lot of “Save” requests to the database. For the procedure, I use the .InsertOrReplace() method, but I get the PK violation a few times an hour. Why is this so? Shouldn’t this method be PK violation safe? Any solutions for this?

Stack trace:
System.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_Orders'. Cannot insert duplicate key in object 'dbo.Orders'. The duplicate key value is (52529786).
The statement has been terminated.
   at Commerce.Orders.Infrastructure.Sql.Repositories.Internal.OrderBaseRepository.Save(Order order, Boolean publishDomainEvents)
   at Commerce.Orders.Infrastructure.Sql.Repositories.OrderRepository.Save(Order order, Boolean publishDomainEvents)
   at Commerce.Orders.Infrastructure.Raven.MigrationRepositories.MigrationOrderRepository.Save(Order order, Boolean publishDomainEvents)
Error Number:2627,State:1,Class:14
            db.InsertOrReplace(convertedOrder);

Environment details

Linq To DB version: 4.2.0

Database (with version): SQL Server 2019

ADO.NET Provider (with version): System.Data.SqlClient 4.8.2

.NET Version: .NET framework 4.8

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, Nov 1, 2022

Serializable should help. Another option could be something like that:

db.Table
    .Merge("HOLDLOCK")
    .Using(new[] { record })
    .OnTargetKey()
    .InsertWhenNotMatched()
    .UpdateWhenMatched()
    .Merge();
0reactions
KlausBenislavskiscommented, Nov 2, 2022

Serializable should help. Another option could be something like that:

db.Table
    .Merge("HOLDLOCK")
    .Using(new[] { record })
    .OnTargetKey()
    .InsertWhenNotMatched()
    .UpdateWhenMatched()
    .Merge();

Why does merge API operations cost so much more? InsertOrReplace was done in 1-3 ms and the code you sent does it in 45-60 ms

Read more comments on GitHub >

github_iconTop Results From Across the Web

Violation of PRIMARY KEY constraint. Cannot insert ...
Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object · 8 · 2 · 1 · what is your unique key...
Read more >
Error "Violation of PRIMARY KEY constraint. Cannot insert ...
It happens when the records you are trying to store inside your Target Data Extension contain the same primary key value more than...
Read more >
Violation of PRIMARY KEY constraint - Microsoft Q&A
Hi I have query as below at the end which when run gives the 'Violation of PRIMARY KEY constraint 'PK_Staff'. Cannot insert duplicate...
Read more >
"Violation of PRIMARY KEY constraint. Cannot insert ...
This error is due to your query returning duplicate values for your primary key column. I assume your primary key column is SubscriberKey....
Read more >
How to solve violation of PRIMARY KEY constraint ...
Open up SSMS and navigate to your table in your database. Expand the keys node and you should see the PK_Teacher. Right click...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found