INSERT 语句与 FOREIGN KEY 约束冲突,即使它为空?

发布于 2024-12-10 01:35:29 字数 1743 浏览 0 评论 0原文

嗨,

我有一个包含外键值(id)的表。该字段可以为 null,但问题是,如果将其设置为 null,则会出现以下异常:

INSERT 语句与 FOREIGN KEY 约束冲突

为什么?它可以为空

Edit1 :

USE [biss]
GO

/****** Object:  Table [dbo].[VoucherCodes]    Script Date: 10/16/2011 19:55:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[VoucherCodes](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Code] [nvarchar](50) NOT NULL,
    [StartDate] [datetime] NOT NULL,
    [EndDate] [datetime] NOT NULL,
    [InactivatedDate] [datetime] NULL,
    [AdCategoryId] [int] NULL,
 CONSTRAINT [PK_VoucherCodes] PRIMARY KEY CLUSTERED 
(
    [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].[VoucherCodes]  WITH CHECK ADD  
   CONSTRAINT [FK_VoucherCodes_AdCategories] FOREIGN KEY([AdCategoryId])
   REFERENCES [dbo].[AdCategories] ([Id])
   ON UPDATE SET NULL
GO

ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_AdCategories]
GO

ALTER TABLE [dbo].[VoucherCodes]  WITH CHECK ADD  
   CONSTRAINT [FK_VoucherCodes_VoucherCodes] FOREIGN KEY([Id])
   REFERENCES [dbo].[VoucherCodes] ([Id])
GO

ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes]
GO

ALTER TABLE [dbo].[VoucherCodes]  WITH CHECK ADD  
   CONSTRAINT [FK_VoucherCodes_VoucherCodes1] FOREIGN KEY([Id])
   REFERENCES [dbo].[VoucherCodes] ([Id])
GO

ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes1]
GO

ALTER TABLE [dbo].[VoucherCodes] ADD  
   CONSTRAINT [DF_VoucherCodes_Inactive]  DEFAULT ((1)) FOR [InactivatedDate]
GO

Hi,

I have a table that contains a foreign key value(id). This field is able to be null but the problem is that I get the following exception if I set it to null :

The INSERT statement conflicted with the FOREIGN KEY constraint

Why? it is nullable

Edit1 :

USE [biss]
GO

/****** Object:  Table [dbo].[VoucherCodes]    Script Date: 10/16/2011 19:55:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[VoucherCodes](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Code] [nvarchar](50) NOT NULL,
    [StartDate] [datetime] NOT NULL,
    [EndDate] [datetime] NOT NULL,
    [InactivatedDate] [datetime] NULL,
    [AdCategoryId] [int] NULL,
 CONSTRAINT [PK_VoucherCodes] PRIMARY KEY CLUSTERED 
(
    [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].[VoucherCodes]  WITH CHECK ADD  
   CONSTRAINT [FK_VoucherCodes_AdCategories] FOREIGN KEY([AdCategoryId])
   REFERENCES [dbo].[AdCategories] ([Id])
   ON UPDATE SET NULL
GO

ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_AdCategories]
GO

ALTER TABLE [dbo].[VoucherCodes]  WITH CHECK ADD  
   CONSTRAINT [FK_VoucherCodes_VoucherCodes] FOREIGN KEY([Id])
   REFERENCES [dbo].[VoucherCodes] ([Id])
GO

ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes]
GO

ALTER TABLE [dbo].[VoucherCodes]  WITH CHECK ADD  
   CONSTRAINT [FK_VoucherCodes_VoucherCodes1] FOREIGN KEY([Id])
   REFERENCES [dbo].[VoucherCodes] ([Id])
GO

ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes1]
GO

ALTER TABLE [dbo].[VoucherCodes] ADD  
   CONSTRAINT [DF_VoucherCodes_Inactive]  DEFAULT ((1)) FOR [InactivatedDate]
GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-12-17 01:35:29

您的外键来自两个自引用循环 FK 约束。

您将 ID 列作为其自身的 FK:它们当然会失败,因为它们在 INSERT 时不存在。

运行这个

ALTER TABLE [dbo].[VoucherCodes]
  DROP CONSTRAINT FK_VoucherCodes_VoucherCodes, FK_VoucherCodes_VoucherCodes1

Your foreign key comes from the two self referencing circular FK constraints.

You have the ID columns as FKs of themselves: of course they will fail because they won't exists at INSERT time.

Run this

ALTER TABLE [dbo].[VoucherCodes]
  DROP CONSTRAINT FK_VoucherCodes_VoucherCodes, FK_VoucherCodes_VoucherCodes1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文