#1452 - 无法添加或更新子行:外键约束失败 [英] #1452 - Cannot add or update a child row: a foreign key constraint fails
问题描述
当我不想将两个表相互连接时,我收到消息:#1452 - 无法添加或更新子行:外键约束失败......我想连接 oauth_uid2(主键)来自facebook_users 表到带有外键 oauth_uid2 的 bugs 表.但我总是收到这条消息.我已经清除了我的数据,但没有任何效果.也在 users_facebook 表中我有 1 条记录.
When I wan't to connect two tables with eachother then I get the message : #1452 - Cannot add or update a child row: a foreign key constraint fails....I want to connect oauth_uid2 (primary key) from the facebook_users tabel to the bugs tabel with the foreign key oauth_uid2. But I get always this message. I have already cleared my data but nothing works.Also in the table users_facebook I have 1 record.
1452 - 无法添加或更新子行:外键约束失败 (phples
.bugs
, CONSTRAINT bugs_ibfk_1
FOREIGN KEY (<代码>oauth_uid2) 参考users_facebook
(oauth_uid2
) 在更新级联时删除级联)
1452 - Cannot add or update a child row: a foreign key constraint fails (
phples
.bugs
, CONSTRAINTbugs_ibfk_1
FOREIGN KEY (oauth_uid2
) REFERENCESusers_facebook
(oauth_uid2
) ON DELETE CASCADE ON UPDATE CASCADE)
错误表:FK = oauth_uid2,PK= bug_id
bugs table: FK = oauth_uid2, PK= bug_id
#Name Type Collation Attributes Null Default Extra Action
1 bug_id int(30) No None AUTO_INCREMENT Change Drop Browse distinct values Primary Unique Index Spatial Fulltext
2 bugtitle varchar(50) utf8_unicode_ci No None Change Drop Browse distinct values Primary Unique Index Spatial Fulltext
3 bugdescription varchar(500) utf8_unicode_ci No None Change Drop Browse distinct values Primary Unique Index Spatial Fulltext
4 oauth_uid2 int(30) No None Change Drop Browse distinct values Primary Unique Index Spatial Fulltext
users_facebook 表:PK= oauth_uid2
users_facebook table: PK= oauth_uid2
# Name Type Collation Attributes Null Default Extra Action
1 oauth_uid2 int(30) No None Change Drop Browse distinct values Primary Unique Index Spatial Fulltext
2 email varchar(70) utf8_unicode_ci No None Change Drop Browse distinct values Primary Unique Index Spatial Fulltext
推荐答案
您已经链接了表格,这就是错误的来源.您需要确保您在 users_facebook
BEFORE 中使用相同的 oauth_uid2 将记录插入到 bugs
中,例如>
You've already got the tables linked, which is where the error is coming from. You need to make sure that you have a record in users_facebook
BEFORE you try to insert a record into bugs
with the same oauth_uid2, e.g.
users_facebook has records with oauth_uid2 `10`, `20`, `30`
您尝试使用
INSERT INTO bugs (oauth_uid2) VALUES (10) // works, there's a matching record in users_facebook
INSERT INTO bugs (oauth_uid2) VALUES (15) // fails, there's no user with that id.
这篇关于#1452 - 无法添加或更新子行:外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!