MySQL 5.5 errno: 150 “Foreign key constraint is incorrectly formed”

前端 未结 3 1621
独厮守ぢ
独厮守ぢ 2021-01-16 20:25

There are plenty of questions regarding this error but I can\'t seem to find any similar scenario to what I have.

My 1st table (users):

My 2nd table

3条回答
  •  长发绾君心
    2021-01-16 20:37

    We would expect to see this error if the types of the primary and foreign key did not match exactly. While both appear to be integer with a width of 1, my guess here is that one of the INT columns in the key relationship is unsigned, while the other is signed. A possible fix would be to make both columns unsigned:

    ALTER TABLE users MODIFY collegelinkId INT(10) UNSIGNED NOT NULL;
    ALTER TABLE college MODIFY id INT(10) UNSIGNED NOT NULL;
    

    Edit:

    I was wrong, as evidenced by your latest comments under my answer. Another possibility is that you created your two tables using different database engines. For example, if you created users using InnoDB, but college using MyISAM, you could still get this error. To fix this, change the engine(s) on the tables to the same type.

    Note that yet another possibility would be that the two columns had different collations. But, that's really a moot point here, since both columns are numeric, not text.

提交回复
热议问题