我有 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