0

我有一個Sql Server數據庫,它是由Nhibernate的模式工具生成的,當我嘗試輸入一個已知的PartyId時觸及下面的錯誤,而我只是沒有得到錯誤。調試外鍵約束錯誤

認爲的FK約束稱PartyId必須存在(即使壽我展示日誌輸出是從使用NHibernate試運行,我可以使用NHib生成的SQL Server數據庫手動複製錯誤)首先,但正如我所說 - 它的確如此。

我有SQL Server 2008管理工作室,但我很少使用它,更喜歡在我需要的那些難得的時候通過Visual Studio訪問它。所以我有兩個基本問題

  • 如何從visual studio中查看sql server中的ddl?
  • 如何調試和修復我的FK約束問題?

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

的錯誤生成的DDL

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

FOREIGN KEY約束定義在哪裏? –

+0

@ypercube。我擔心這是問題的一部分 - 我如何在Visual Studio或SQL Server Mgt Studio中找到它? – Berryl

+0

在Mgt Studio(對象資源管理器)中,您可以「右鍵單擊」一個表格 - >腳本表格 - > CREATE To - >剪貼板(或文件或編輯器窗口) –

回答

3

外鍵可能是不正確定義。這是目前的定義。

​​

它應該可能如下所示。

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

請注意,我爲PartyId交換了PartyNameId。

+0

grrrr - 謝謝!!! – Berryl