好的,也许是晚了,我很愚蠢,但是我似乎无法弄清楚为什么在以下查询中出现Cannot add Foreign Key Constraint
错误
DROP TABLE IF EXISTS People_Lordships;
CREATE TABLE People_Lordships
(
Id INT PRIMARY KEY AUTO_INCREMENT,
PersonId INT NOT NULL,
LordshipId INT NOT NULL,
AssumedDate Date,
AbdicatedDate Date
);
DROP TABLE IF EXISTS People_Lordships_Current;
CREATE TABLE People_Lordships_Current
(
Id INT PRIMARY KEY AUTO_INCREMENT,
People_LordshipsId INT NOT NULL,
LordShipId INT NOT NULL,
CONSTRAINT Fk_People_Lordships_Current_People_LordshipsId_LordshipId
FOREIGN KEY (`LordshipId`,`People_LordshipsId`)
REFERENCES People_Lordships (`LordshipId`,`Id`)
ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT UQ_People_Lordships_Current_LordshipId
UNIQUE KEY (`LordshipId`)
);
是的,这是一个有关贵族头衔的数据库...这是一个漫长的故事
最佳答案
表LordshipId
中没有列People_Lordships
。
您的外键定义尝试引用不存在的列。
REFERENCES People_Lordships (`LordshipId`,`Id`)
^^^^^^^^^^^^
关于mysql - 令人困惑的无法添加外键约束错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29713073/