引用的 table 中没有主键或候选键......与外键中的引用列列表匹配

There are no primary or candidate keys in the referenced table ... that match the referencing column list in the foreign key

我在创建 table [dbo].[WeibullSummaryDetails].

时遇到此错误

这是我的两个 table

CREATE TABLE [dbo].[WeibullFilterDetails]
(
        [WeibullFilterDetailsId] [int] IDENTITY(1,1) NOT NULL,
        [ProjectTeamId] int not null,
        [WeekStartDate] date not NULL,
        [WeekEndDate] date  not null ,
        [IsRefreshed] bit NULL,

        CONSTRAINT FK_WeibullFilterDetails_WeibullFilterDetails 
            FOREIGN KEY ([ProjectTeamId])
            REFERENCES [dbo].[ProjectTeams]([Id]),
        PRIMARY KEY ([ProjectTeamId], [WeibullFilterDetailsId])
) 

CREATE TABLE [dbo].[WeibullSummaryDetails]
(
    [WeibullSummaryDetailsId] [int] IDENTITY(1,1) NOT NULL,
    [WeibullFilterDetailsId] int not null,
    [ProjectTeamId] int not null,
    [ActualEstimatedBugCount] int NULL,
    [CurrentBugCount] int NULL,
    [PercentageBugFound] float NULL,
    [PercentageBugResolved] float NULL,
    [BugsToFind] int NULL,
    BugsToResolve int NULL,
    LinearEquation nvarchar(100) null,
    RSquare float NULL,
    Shape float NULL,
    Scale float NULL

    PRIMARY KEY ([WeibullSummaryDetailsId], [WeibullFilterDetailsId],[ProjectTeamId]),
    CONSTRAINT FK_WeibullSummaryDetails_WeibullFilterDetails 
        FOREIGN KEY ([WeibullFilterDetailsId],[ProjectTeamId])
        REFERENCES [dbo].[WeibullFilterDetails]([WeibullFilterDetailsId],[ProjectTeamId])
) 

详细的错误信息

Msg 1776, Level 16, State 0, Line 14
There are no primary or candidate keys in the referenced table 'dbo.WeibullFilterDetails' that match the referencing column list in the foreign key 'FK_WeibullSummaryDetails_WeibullFilterDetails'.

Msg 1750, Level 16, State 0, Line 14
Could not create constraint. See previous errors.

我看过其他关于这个错误的帖子,通常给出的解决方案是如果父 table 有一个复合键,那么两个列也应该出现在子 table 中并且应该用于外键约束。

这正是我在这里所做的,但仍然出现此错误。

非常感谢帮助!

对于 [dbo].[WeibullFilterDetails] 您将主键定义为 ([ProjectTeamId],[WeibullFilterDetailsId]),但在您的 REFERENCES 子句中您写了 ([WeibullFilterDetailsId],[ProjectTeamId]) -- 顺序不匹配。尝试:

CREATE TABLE [dbo].[WeibullSummaryDetails](
    ...
    FOREIGN KEY ([ProjectTeamId],[WeibullFilterDetailsId])
    REFERENCES [dbo].[WeibullFilterDetails]([ProjectTeamId],[WeibullFilterDetailsId])
);