无法添加或更新子行:外键约束失败

2021-11-20 00:00:00 mysql

表 #1:

<头>
字段类型默认额外
用户名int(11)没有PRINULL自动增量
密码varchar(20)没有
用户名varchar(25)没有
电子邮件varchar(60)没有

表 #2:

<头>
字段类型默认额外
用户名int(11)没有
帖子IDint(11)没有PRINULL自动增量
标题varchar(50)没有
总结varchar(500)没有

错误:

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(myapp/table2, CONSTRAINT table2_ibfk_1 外键 (UserID)参考 table1 (UserID))

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (myapp/table2, CONSTRAINT table2_ibfk_1 FOREIGN KEY (UserID) REFERENCES table1 (UserID))

我做错了什么?我阅读了 http://www.w3schools.com/Sql/sql_foreignkey.asp 并且我不知道有什么问题.

What have I done wrong? I read http://www.w3schools.com/Sql/sql_foreignkey.asp and I don't see what's wrong.

推荐答案

您收到此错误是因为您尝试向 table2 添加/更新没有有效值的行UserID 字段基于当前存储在 table1 中的值.如果您发布更多代码,我可以帮助您诊断具体原因.

You're getting this error because you're trying to add/update a row to table2 that does not have a valid value for the UserID field based on the values currently stored in table1. If you post some more code I can help you diagnose the specific cause.

相关文章