Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

The INSERT statement conflicted with the FOREIGN KEY constraint(Not reproducible) Expand / Collapse
Author
Message
Posted Friday, August 14, 2009 3:59 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 5, 2010 5:37 PM
Points: 8, 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.
Post #771369
Posted Monday, August 17, 2009 6:41 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:42 PM
Points: 10,340, Visits: 13,341
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

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
Post #771848
Posted Monday, August 17, 2009 2:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 5, 2010 5:37 PM
Points: 8, 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.
Post #772248
Posted Monday, August 17, 2009 2:41 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 5, 2010 5:37 PM
Points: 8, 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).
Post #772273
Posted Monday, August 17, 2009 2:59 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:42 PM
Points: 10,340, Visits: 13,341
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

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
Post #772291
Posted Monday, August 17, 2009 5:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 5, 2010 5:37 PM
Points: 8, 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.
Post #772374
Posted Wednesday, August 19, 2009 12:14 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 5, 2010 5:37 PM
Points: 8, 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.
Post #773804
Posted Wednesday, October 28, 2009 11:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 5, 2010 5:37 PM
Points: 8, 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
Post #810184
Posted Wednesday, October 28, 2009 12:01 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:42 PM
Points: 10,340, Visits: 13,341
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

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
Post #810192
Posted Tuesday, January 5, 2010 5:39 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 5, 2010 5:37 PM
Points: 8, 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.
Post #842507
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse