Berryl
Berryl

Reputation: 12863

debug foreign key constraint error

I have a Sql Server db that was generated by Nhibernate's schema tool and hitting the error below when I try to enter a known PartyId, and I just do not get the error. (Even tho the log output I'm showing is from a test run using NHibernate, I can replicate the error manually using the SQL Server db that NHib generated)

I think the FK constraint is saying the PartyId must exist first, but like I say - it does.

I have SQL Server 2008 management studio but I rarely use it, preferring to access it through Visual Studio on those rare times I need to. So I have two basic questions

DDL generated by NHib

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)
)

The ERROR

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'.

Create Scripts

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

Upvotes: 0

Views: 2087

Answers (1)

Scott Munro
Scott Munro

Reputation: 13596

The foreign key is probably not defined correctly. This is the current definition.

ALTER TABLE [dbo].[PartyNames]  WITH CHECK ADD  CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyNameId])REFERENCES [dbo].[Parties] ([PartyId])

It should probably be as follows.

ALTER TABLE [dbo].[PartyNames]  WITH CHECK ADD  CONSTRAINT [Party_PartyName_FK] FOREIGN KEY([PartyId])REFERENCES [dbo].[Parties] ([PartyId])

Note that I have swapped PartyNameId for PartyId.

Upvotes: 3

Related Questions