Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 The INSERT statement conflicted with the FOREIGN K

Author  Topic 

CoffeeAddict
Yak Posting Veteran

94 Posts

Posted - 2009-12-01 : 14:02:15
Error: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Item__order__3AE27131". The conflict occurred in database "pmall", table "dbo.ItemSaved", column 'id'.

Here's my table:

ItemSavedUnits
---------
id
ItemID (is set in this table a FK to Item.id)
..etc.

Here's my insert statement:


insert into ItemSavedUnits (ItemID, name, Price)
select 92439, name,Price
from ItemUnits where ItemID = 92439


I don't really understand why if I a FK constraint on ItemSavedUnits.ItemID that is related to Item.ItemID and ItemUnits has no constraints at all why I'm getting a problem inserting into ItemSavedUnits. The ItemID I'm tryign to insert does exist in the Item table.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-01 : 14:54:36
You are attempting to insert a row into a child table and the parent table does not have an associated row yet. You must insert into the parent first and then the child. Check who is the parent and modify your code appropriately. The way to check is by looking at the FK__Item__order__3AE27131 constraint.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

CoffeeAddict
Yak Posting Veteran

94 Posts

Posted - 2009-12-01 : 15:06:31
figured out whoever added this FK fucked up. They named it right but pointed it to the wrong table!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-01 : 15:24:03
Cool, glad you got it sorted out.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -