1

I have a big bunch of inserts that I do through DataContext in transaction. I inserting two types of objects: A and B.

I do execute many sql inserts like:

INSERT INTO A (...) VALUES ('11111111-1111-1111-1111-111111111111',..)
INSERT INTO B (...) VALUES (
   '21111111-1111-1111-1111-111111111111', 
   '11111111-1111-1111-1111-111111111111',...) 

separately in

db.ExecuteCommand(sql.ToString(), processedParams.ToArray());

table B have foreign key to A.

Sometimes I get error like: violation of FOREIGN KEY constraint "FK_B_A" on table "A". Column "GUID".

Sometimes all records successfully inserts in to DB. Data bunch always the same except dynamically generated GUIDs.

It looks like the insert in to B sometimes occurs before inserts to A.

5
  • Your error is giving away what's wrong.. within your code are you doing any Commits.. or are you doing like bulk Inserts and then a commit.. when doing via transactions remember the error will not be apparent until the commit call.. I would suggest doing single inserts and commits to figure out what duplicate key you are trying to insert.. are you using the C# method to generate and return new GIUD's those are generally unique keys so if you are hard coding and trying to insert the same key... well that will generate a foreign key constraint.. Commented Dec 19, 2011 at 17:00
  • You know the SQL inserts you have up there are incorrect right? INSERT INTO A (column names) VALUES(actual values) . A code sample from where you build your query strings might help locate this problem. Commented Dec 19, 2011 at 17:09
  • Sorry I just mistake when write sample. Sometimes all records successfully inserts in to DB. Commented Dec 19, 2011 at 17:09
  • Keys for records generates automaticly and always rights. The problem not with duplicate guids. Some time I have the violation of foregin key. Commented Dec 20, 2011 at 8:05
  • This error throws exactly when db.ExecuteCommand. I supose, because it gets in transaction. Commented Dec 20, 2011 at 8:08

2 Answers 2

4

The order might be swapped somehow.

I recomend you to first do all inserts in table A and only after that do inserts on table B.


Also, it seems that the inserts that you posted are incorrect.

See here for examples.

Sign up to request clarification or add additional context in comments.

6 Comments

When I rewrite program and do all inserts in to table A before all inserts into table B. The error reproduces always.
But if I try to insert small count of records (10-20) records all ok.
That's kinda weird. Do you commit the inserts on table A before making the inserts on table B?
No. I need to commit transaction only after all inserts. And rollback if somthing wrong.
That will give the same problem. The order of execution might not be the one that you think. You really need to make 2 commits.
|
1

Thank you All for help.

It was side effect of bug in transaction cleanup process in my service. It rollback any long very long transaction. Error be thrown when cleanup occurs between insert into table A and table B, becasue record in A no longer existed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.