Cannot add or update a child row: a foreign key constraint fails

table #1:

FieldTypeNullKeyDefaultExtra
UserIDint(11)NOPRINULLauto_increment
Passwordvarchar(20)NO
Usernamevarchar(25)NO
Emailvarchar(60)NO

table #2:

FieldTypeNullKeyDefaultExtra
UserIDint(11)NOMUL
PostIDint(11)NOPRINULLauto_increment
Titlevarchar(50)NO
Summaryvarchar(500)NO

Error:

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))

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

Leave a Comment

tech