INSERT 语句与 FOREIGN KEY 约束冲突

2021-12-13 00:00:00 tsql sql-server-2008 sql-server

错误:System.Data.SqlClient.SqlException:INSERT 语句与 FOREIGN KEY 约束FK__Item__order__3AE27131"冲突.冲突发生在数据库pmall"、表dbo.ItemSaved"、列id"中.

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'.

这是我的桌子:

  • id
  • ItemID(在此表中设置为 Item.id 的 FK)
  • ...等

这是我的插入语句:

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

我真的不明白为什么如果我对与 Item.ItemID 和 ItemUnits 相关的 ItemSavedUnits.ItemID 的 FK 约束根本没有约束,为什么我在插入 ItemSavedUnits 时遇到问题.我尝试插入的 ItemID 确实存在于 Item 表中.

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.

推荐答案

您确定 ItemId 92439 存在于 Item 表中,而不仅仅是 ItemUnits 中吗?

Are you absolutely sure that ItemId 92439 exists in the Item table, and not just in ItemUnits?

您的 select 语句是否返回 null?

Is your select statement returning null?

相关文章