此问题已在此处有答案:
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'donor_ibfk_1' in the referenced table 'BloodStorage' [closed](1个答案)
Error Code: 1822. Failed to add the foreign key constaint. Missing index for constraint(14个回答)
3天前关闭。
我尽力了
CREATE TABLE `valorant`.`abilities` (
`PlayerId` INT NOT NULL,
`AgentName` VARCHAR(45) NOT NULL,
`AbilityName` VARCHAR(45) NOT NULL,
`KeyBind` VARCHAR(45) NULL,
`Cost` INT NULL,
PRIMARY KEY (`PlayerId`, `AgentName`, `AbilityName`));
CREATE TABLE `valorant`.`abilitystats` (
`PlayerId` INT NOT NULL,
`AgentName` VARCHAR(45) NOT NULL,
`AbilityName` VARCHAR(45) NOT NULL,
`EnemiesAffected` INT NULL,
`AlliesAffected` INT NULL,
CONSTRAINT `FK_abilitystats_abilities`
FOREIGN KEY (`AbilityName`, `AgentName`)
REFERENCES `abilities` ( `AgentName`, `AbilityName`),
PRIMARY KEY (`PlayerId`)
);
这是我得到的错误
错误代码:1822未能添加外键约束。引用的表“abilities”中缺少约束“FK_abilitystats_abilities”的索引
我不知道这有什么问题。我遵循了FOREIGN KEY
教程
2条答案
按热度按时间vsnjm48y1#
就像@Barmar说的主键以PlayerId开头,所以AgentName,AbilityName本身不是一个键。我所做的就是将
PlayerId
移动到主键(AgentName
,AbilityName
,PlayerId
)的末尾;pb3skfrl2#
您需要添加一个与外键匹配的索引:
请注意,通常情况下,外键引用另一个表的主键会更好。