简体   繁体   English

插入语句与外键约束冲突-SQL Server 2012

[英]The insert statement conflicted with the foreign key constraint - SQL Server 2012

I have 3 tables : Application, Control and Application Control which is basically a many to many relationship between Application and Control. 我有3个表:Application,Control和Application Control,这基本上是Application和Control之间的多对多关系。

App table 应用表

App_ID int (PK)
.....

ApplicationControl table ApplicationControl表

FK_APP_ID int (PK)
FK_Controls_ID (PK)

and Control table 和控制表

ID int (PK)
Name varchar(50)     

Both App and Control tables are populated with some entries. App表和Control表都填充了一些条目。 When I try to insert an entry in the ApplicationControl table (taking an id from App and an ID from Control) it says: 当我尝试在ApplicationControl表中插入一个条目(从App获取ID和从Control获取ID)时,它说:

The Insert statement conflicted with the FOREIGN KEY constraint 
(between ApplicationControl and Control) The conflict occured in table Control,
column ID. 

Any ideas why? 有什么想法吗? It can insert the Application ID but It can't insert the Control ID and there aren't any differences between these two relations. 它可以插入应用程序ID,但不能插入控件ID,并且这两个关系之间没有任何区别。

USE [PicknickDB]
GO

/****** Object:  Table [dbo].[ApplicationControl]    Script Date: 7/10/2013 3:21:29 PM  ******/ 
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ApplicationControl](
[FK_App_ID] [int] NOT NULL,
[FK_Controls_ID] [int] NOT NULL,
CONSTRAINT [PK_ApplicationControl] PRIMARY KEY CLUSTERED 
(
[FK_App_ID] ASC,
[FK_Controls_ID] ASC
 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,      ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 ) ON [PRIMARY]

 GO

 ALTER TABLE [dbo].[ApplicationControl]  WITH CHECK ADD  CONSTRAINT       [FK_MapAppControls_Applications] FOREIGN KEY([FK_App_ID])
REFERENCES [dbo].[Application] ([App_ID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[ApplicationControl] CHECK CONSTRAINT   [FK_MapAppControls_Applications]
GO

ALTER TABLE [dbo].[ApplicationControl]  WITH CHECK ADD  CONSTRAINT [FK_MapAppControls_Controls] FOREIGN KEY([FK_App_ID])
REFERENCES [dbo].[Control] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[ApplicationControl] CHECK CONSTRAINT [FK_MapAppControls_Controls]
GO
ALTER TABLE [dbo].[ApplicationControl]  WITH CHECK ADD  CONSTRAINT 

[FK_MapAppControls_Controls] FOREIGN KEY([FK_App_ID]) --Really?

REFERENCES [dbo].[Control] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE

So, FK_App_ID is used as a reference to both the Application and the Control table? 那么, FK_App_ID是否同时用作Application Control表的引用?

I'd suggest that that should be FK_Controls_ID instead. 我建议应该改为FK_Controls_ID

In cases like this it is best to turn on Profiler and see exactly what values it is trying to insert. 在这种情况下,最好打开Profiler并确切查看它试图插入的值。 This may lead to seeing why there is a problem. 这可能会导致了解为什么存在问题。 My most likely guess is that the value being sent to the insert for the second table is NULL because the code forgot to set it somehwere. 我最可能的猜测是,发送到第二张表的插入项的值为NULL,因为代码忘记了对其进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM