我有一个由Nhibernate的架构工具生成的Sql Server数据库,当我尝试输入已知的PartyId时遇到下面的错误,我只是没有得到错误。 (即使我显示的日志输出来自使用NHibernate的测试运行,我也可以使用NHib生成的SQL Server数据库手动复制错误)
我认为 FK约束是说PartyId必须先存在,但就像我说的那样 - 确实存在。
我有SQL Server 2008管理工作室,但我很少使用它,更喜欢在我需要的极少数时间通过Visual Studio访问它。所以我有两个基本问题
create table PartyNames (
PartyNameId INTEGER not null,
PartyId INTEGER not null,
RequiredName TEXT not null,
EverythingElse TEXT,
ContextUsed TEXT,
Salutation TEXT,
EffectiveStart DATETIME,
EffectiveEnd DATETIME,
primary key (PartyNameId)
)
create table Parties (
PartyId INTEGER not null,
Type TEXT not null,
primary key (PartyId)
)
NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', @p0);@p0 = 98304 [Type: Int32 (0)]
NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', @p0);@p0 = 98305 [Type: Int32 (0)]
NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', @p0);@p0 = 98306 [Type: Int32 (0)]
NHibernate: select next_hi from hibernate_unique_key with (updlock, rowlock)
NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 5 [Type: Int32 (0)], @p1 = 4 [Type: Int32 (0)]
NHibernate: INSERT INTO
PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId)
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);
@p0 = 98304 [Type: Int32 (0)],
@p1 = 'Hesh' [Type: String (50)],
@p2 = 'Berryl;;;' [Type: String (4000)],
@p3 = 'Stack Overflow Profile' [Type: String (50)],
@p4 = 'Fellow Geek' [Type: String (20)],
@p5 = 7/29/2011 4:55:19 PM [Type: DateTime (0)],
@p6 = 12/31/9999 11:59:59 PM [Type: DateTime (0)],
@p7 = 131072 [Type: Int32 (0)]
Test 'Parties.Data.Impl.NHib.Tests.TestFixtures.BaseDataTestFixtures.SqlServerCommandExecutor.GenerateTestData' failed: NHibernate.Exceptions.GenericADOException : could not insert: [Parties.Domain.Names.PartyName#131072][SQL: INSERT INTO PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId) VALUES (?, ?, ?, ?, ?, ?, ?, ?)]
----> System.Data.SqlClient.SqlException :
The INSERT statement conflicted with the FOREIGN KEY constraint "Party_PartyName_FK".
The conflict occurred in database "PartyDomainDb", table "dbo.Parties", column 'PartyId'.
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PartyNames](
[PartyNameId] [int] NOT NULL,
[PartyId] [int] NOT NULL,
[RequiredName] [nvarchar](50) NOT NULL,
[EverythingElse] [nvarchar](255) NULL,
[ContextUsed] [nvarchar](50) NULL,
[Salutation] [nvarchar](20) NULL,
[EffectiveStart] [datetime] NULL,
[EffectiveEnd] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[PartyNameId] 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].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_FK] FOREIGN KEY([PartyId])
REFERENCES [dbo].[Parties] ([PartyId])
GO
ALTER TABLE [dbo].[PartyNames] CHECK CONSTRAINT [Party_FK]
GO
ALTER TABLE [dbo].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyNameId])
REFERENCES [dbo].[Parties] ([PartyId])
GO
ALTER TABLE [dbo].[PartyNames] CHECK CONSTRAINT [Party_PartyName_FK]
GO
USE [PartyDomainDb]
GO
/****** Object: Table [dbo].[Parties] Script Date: 07/29/2011 18:22:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Parties](
[PartyId] [int] NOT NULL,
[Type] [nvarchar](255) NOT NULL,
PRIMARY KEY CLUSTERED
(
[PartyId] 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
答案 0 :(得分:3)
外键可能未正确定义。这是目前的定义。
ALTER TABLE [dbo].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyNameId])REFERENCES [dbo].[Parties] ([PartyId])
应该如下。
ALTER TABLE [dbo].[PartyNames] WITH CHECK ADD CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyId])REFERENCES [dbo].[Parties] ([PartyId])
请注意,我已将PartyNameId换成PartyId。