问题描述
我试图将外键添加到现有表中,并遇到问题。我想我的语法错误,所以我更新了我的hibernate.cfg.xml文件以自动更新。
事实证明,hibernate具有相同的功能错误。这里是我的SQL添加外键:
pre code $ alter table pbi add index FKEA3F7BDE9BAB051(FK_idP),添加约束FKEA3F7BDE9BAB051外键(FK_idP )引用p(idP)
,错误是:
无法添加或更新子行:外键约束失败(`db`.`#sql-6f8_3`,CONSTRAINT`FKEA3F7BDE9BAB051` FOREIGN KEY(`fk_idP`))参考`p`(`idP`))
任何人都能想到这会失败的原因吗?
此错误意味着无法应用约束,因为存在违反它的现有记录。
$ b
在你的情况下, pbi
表中的行的 FK_idP
列的值为其中 idP
列中的 p
表中没有与该值匹配的记录。
I'm trying to add a foreign key to an existing table, and was having issues. I figured that I had an error in my syntax, so I updated my hibernate.cfg.xml file to auto-update.
As it turns out, hibernate had the same error. Here's my SQL to add the foreign key:
alter table pbi add index FKEA3F7BDE9BAB051 (FK_idP), add constraint FKEA3F7BDE9BAB051 foreign key (FK_idP) references p (idP)
and the error is:
Cannot add or update a child row: a foreign key constraint fails (`db`.`#sql-6f8_3`, CONSTRAINT `FKEA3F7BDE9BAB051` FOREIGN KEY (`fk_idP`) REFERENCES `p` (`idP`))
Can anyone think of a reason why this would fail?
This error means that constraint can not be applied because there are existing records that would violate it.
In your case, pbi
table has rows whose FK_idP
column has a value for which there are no matching records with that value in idP
column of p
table.
这篇关于无法创建外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!