INSERT语句与FOREIGN KEY约束冲突的问题
The INSERT statement conflicted with the FOREIGN KEY constraint problem
我有下面的脚本,它给了我一个错误:
"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.PlanShiftAssignments_dbo.User_UserId”。冲突发生在数据库 "SWS"、table "dbo.User"、列 'Id' 中。
声明已终止。"
如您所见,在 WHERE 子句中我检查 UserId 是否存在于 dbo.User 中。错误的其他可能原因是什么?
更新: 我还想知道 select 语句中的哪一行导致了错误。任何有关调试此查询的建议都将不胜感激。我正在使用 MS SQL Server Management Studio。
CREATE TABLE [dbo].[PlanShiftAssignments] (
[PlanShiftId] [uniqueidentifier] NOT NULL,
[Status] [int] NOT NULL,
[UserId] [int],
CONSTRAINT [PK_dbo.PlanShiftAssignments] PRIMARY KEY ([PlanShiftId])
)
CREATE INDEX [IX_PlanShiftId] ON [dbo].[PlanShiftAssignments]([PlanShiftId])
CREATE INDEX [IX_UserId] ON [dbo].[PlanShiftAssignments]([UserId])
ALTER TABLE [dbo].[PlanShiftAssignments] ADD CONSTRAINT [FK_dbo.PlanShiftAssignments_dbo.PlanShifts_PlanShiftId] FOREIGN KEY ([PlanShiftId]) REFERENCES [dbo].[PlanShifts] ([Id])
ALTER TABLE [dbo].[PlanShiftAssignments] ADD CONSTRAINT [FK_dbo.PlanShiftAssignments_dbo.User_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
insert into dbo.PlanShiftAssignments
select ps.Id as PlanShiftId, ISNULL(ps.AssigneeId, psi.UserId) as UserId, ISNULL(psi.[Status], 1) as [Status] from dbo.PlanShifts ps
left join
dbo.PlanShiftInvitations psi
on ps.Id = psi.PlanShiftId
where (psi.UserId is not null and psi.UserId IN (select Id from dbo.[User]))
or (ps.AssigneeId is not null and ps.AssigneeId IN (select Id from dbo.[User]))
可能是因为您在 WHERE 子句中指定了 OR,因此 AssigneeId 或 UserId 在用户 table 中而不是另一个,因此使 FK 约束无效。
您的数据模型很奇怪,如果 UserId
和 AssigneeId
尚未在基础表中引用 User
。
无论如何,您的 where
子句是
where (psi.UserId is not null and psi.UserId IN (select Id from dbo.[User])) or
(ps.AssigneeId is not null and ps.AssigneeId IN (select Id from dbo.[User]))
这留下了 psi.UserId
匹配但 ps.AssigneeId
不匹配的可能性。
为确保逻辑匹配,请使用与 select
中相同的表达式:
where coalesce(ps.AssigneeId, psi.UserId) in (select Id from dbo.[User])
确保您始终在每个INSERT
语句中包含目标的列列表。
insert into dbo.PlanShiftAssignments (
PlanShiftId,
UserId,
Status)
SELECT
ps.Id as PlanShiftId,
ISNULL(ps.AssigneeId, psi.UserId) as UserId,
ISNULL(psi.[Status], 1) as [Status]
...
您的 table 是使用顺序 PlanShiftId, Status, UserId
创建的,而您当前 SELECT
的列顺序是 PlanShiftId, UserId, Status
,因此造成了混淆。
我有下面的脚本,它给了我一个错误: "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.PlanShiftAssignments_dbo.User_UserId”。冲突发生在数据库 "SWS"、table "dbo.User"、列 'Id' 中。 声明已终止。"
如您所见,在 WHERE 子句中我检查 UserId 是否存在于 dbo.User 中。错误的其他可能原因是什么?
更新: 我还想知道 select 语句中的哪一行导致了错误。任何有关调试此查询的建议都将不胜感激。我正在使用 MS SQL Server Management Studio。
CREATE TABLE [dbo].[PlanShiftAssignments] (
[PlanShiftId] [uniqueidentifier] NOT NULL,
[Status] [int] NOT NULL,
[UserId] [int],
CONSTRAINT [PK_dbo.PlanShiftAssignments] PRIMARY KEY ([PlanShiftId])
)
CREATE INDEX [IX_PlanShiftId] ON [dbo].[PlanShiftAssignments]([PlanShiftId])
CREATE INDEX [IX_UserId] ON [dbo].[PlanShiftAssignments]([UserId])
ALTER TABLE [dbo].[PlanShiftAssignments] ADD CONSTRAINT [FK_dbo.PlanShiftAssignments_dbo.PlanShifts_PlanShiftId] FOREIGN KEY ([PlanShiftId]) REFERENCES [dbo].[PlanShifts] ([Id])
ALTER TABLE [dbo].[PlanShiftAssignments] ADD CONSTRAINT [FK_dbo.PlanShiftAssignments_dbo.User_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
insert into dbo.PlanShiftAssignments
select ps.Id as PlanShiftId, ISNULL(ps.AssigneeId, psi.UserId) as UserId, ISNULL(psi.[Status], 1) as [Status] from dbo.PlanShifts ps
left join
dbo.PlanShiftInvitations psi
on ps.Id = psi.PlanShiftId
where (psi.UserId is not null and psi.UserId IN (select Id from dbo.[User]))
or (ps.AssigneeId is not null and ps.AssigneeId IN (select Id from dbo.[User]))
可能是因为您在 WHERE 子句中指定了 OR,因此 AssigneeId 或 UserId 在用户 table 中而不是另一个,因此使 FK 约束无效。
您的数据模型很奇怪,如果 UserId
和 AssigneeId
尚未在基础表中引用 User
。
无论如何,您的 where
子句是
where (psi.UserId is not null and psi.UserId IN (select Id from dbo.[User])) or
(ps.AssigneeId is not null and ps.AssigneeId IN (select Id from dbo.[User]))
这留下了 psi.UserId
匹配但 ps.AssigneeId
不匹配的可能性。
为确保逻辑匹配,请使用与 select
中相同的表达式:
where coalesce(ps.AssigneeId, psi.UserId) in (select Id from dbo.[User])
确保您始终在每个INSERT
语句中包含目标的列列表。
insert into dbo.PlanShiftAssignments (
PlanShiftId,
UserId,
Status)
SELECT
ps.Id as PlanShiftId,
ISNULL(ps.AssigneeId, psi.UserId) as UserId,
ISNULL(psi.[Status], 1) as [Status]
...
您的 table 是使用顺序 PlanShiftId, Status, UserId
创建的,而您当前 SELECT
的列顺序是 PlanShiftId, UserId, Status
,因此造成了混淆。