0

我有 3 个表:Application、Control 和 Application Control,这基本上是 Application 和 Control 之间的多对多关系。

应用表

App_ID int (PK)
.....

应用控制表

FK_APP_ID int (PK)
FK_Controls_ID (PK)

和控制表

ID int (PK)
Name varchar(50)     

App 和 Control 表都填充了一些条目。当我尝试在 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. 

任何想法为什么?它可以插入Application ID,但不能插入Control 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
4

2 回答 2

3
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

那么,FK_App_ID是同时作为table和table的引用Application Control

我建议应该FK_Controls_ID改为。

于 2013-07-10T14:49:50.320 回答
0

在这种情况下,最好打开 Profiler 并准确查看它试图插入的值。这可能会导致看到为什么会出现问题。我最有可能的猜测是,发送到第二个表的插入的值是 NULL,因为代码忘记设置它。

于 2013-07-10T14:44:45.710 回答