Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Violation of UNIQUE KEY Contstraints - Cannot insert duplicate key

I am pulling out my hair over here. I have a table that has a UNIQUE Key (IX_tblTable) and the unique key is on a column Number. I am parsing some data from the web and storing it in the table. My latest collection of data from the web contains number THAT ARE NOT CONTAINED IN THE DATABASE. so I am getting data from the site and all of the data I am getting in unique, there are no duplicates and the numbers in the list that is returned are not in the database.

I keep getting this error everytime I try to update the database, what is the best way to trap the error to see which number is throwing the error. I am storing everything that comes back in an object list and when it is done running I have 131 records that need to be inserted and I can not see which one is throwing this error. What is the bast way to trap it?

EDIT: I am using sql server 2005, wirtting in C# and using Linq2SQL. I can not post any c# code at this time for proprietary reasons...

like image 323
EvanGWatkins Avatar asked Sep 30 '10 12:09

EvanGWatkins


2 Answers

Can you just disable your constraint for a while and see what duplicates save? Later you can remove duplicates and re-enable it.

like image 104
A-K Avatar answered Oct 25 '22 07:10

A-K


Create a copy of the table without a primary key or uniqueness constraint (having the column, just not as a primary). Modify your code to insert into that table. Run it. Select values having more than one duplicate.

like image 26
Jon Hanna Avatar answered Oct 25 '22 08:10

Jon Hanna