SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


The INSERT statement conflicted with the FOREIGN KEY constraint(Not reproducible)


The INSERT statement conflicted with the FOREIGN KEY constraint(Not reproducible)

Author
Message
anki_bakshi
anki_bakshi
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 30
We have one parent table:
Document (GID int identity(1,1),ParentName)

and two child tables, both having FOREIGN KEY(Document_GID) REFERENCES Document (GID)
1. DocumentChild1(Document_GID,Child1_name)
2. DocumentChild2(Document_GID,Child2_name)

Steps:
1. Lets say DocumentChild1 table has 'n' documents.
2. Then in a transaction we try to insert entries into DocumentChild2 from DocumentChild1.
Here, we get foreign key violation :
The INSERT statement conflicted with the FOREIGN KEY constraint "DocumentChild2_DOC_GID_FK".
The conflict occurred in database "VOL_ABC", table "Document", column 'GID'.

Also, we don't have any delete operation in Document table. But many insert operation into this table can be going on in parallel.
We saw this error once. Everything works fine when we reran the insert sql.

I am wondering, what could have caused this behavior. Definitely data exists in the Document table else insert into DocumentChild1 would have failed.

Let me know if you need more details.
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18270 Visits: 14889
You aren't using nolock or SET ISOLATION LEVEL READ UNCOMMITTED are you?



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
anki_bakshi
anki_bakshi
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 30
My transaction isolation level is READ_COMMITTED and yes, I am using nolocks while reading data from DocumentChild1. But, I have verified that no one is changing(Insert/Update/Delete) data in the DocumentChild1 during that time. Also, I had sql profiler enable when we saw this error. The rowcount suggests that the correct number of records being read from DocumentChild1.

Thanks for your help.
anki_bakshi
anki_bakshi
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 30
I am wondering do indexes on the table play part in this FK constraint violation error.
I have a unique index on DocumentChild2(Document_GID,Child2_name).
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18270 Visits: 14889
anki_bakshi (8/17/2009)
I am wondering do indexes on the table play part in this FK constraint violation error.
I have a unique index on DocumentChild2(Document_GID,Child2_name).


Nope, you'd get a unique index/constraint violation error.

Was the FK on DocumentChild1 created AFTER there was data in the table? If to there could be "bad" data in the table if the constraint was created with the NOCHECK option



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
anki_bakshi
anki_bakshi
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 30
No, the FK on DocumentChild1 was created at the time of table creation.

Can multiple asynchronous inserts in the Document table can cause this behavior? I mean may be the index page where the concerned GID is located is locked by the Insert sql as it is also inserting into the same page.
anki_bakshi
anki_bakshi
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 30
Update:
We had a live meeting session with microsoft support where they have debugged this behavior. There is no resolution yet, and might be a bug on their side.
anki_bakshi
anki_bakshi
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 30
Update: There is a bug in microsoft sql server's Hash Map join plan when it comes to # based temp tables.
To suppress this error(workaround given by Microsoft team): Include order by 1 clause - this will change the join plan.

For eg.
INSERT INTO MAIN_TABLE (DOC_ID_FK_TO_SOME_OTHER_TABLE, ANY_COLUMN)
SELECT DOC_ID_FK_TO_SOME_OTHER_TABLE, ANY_COLUMN
FROM #TEMP_TABLE -- This will sometimes throw FK violation which is not a --valid violation
ORDER BY 1 -- This suppresses the error
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18270 Visits: 14889
Interesting. Thanks for posting the resolution even though it came months later.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
anki_bakshi
anki_bakshi
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 30
Here is KB article ( http://support.microsoft.com/kb/977100/ )explaining about the issue we have raised and possible workarounds in case anyone is interested.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search