Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Created February 23, 2016 13:44
Show Gist options
  • Save kirilkirkov/58c3c5fbd90729088b43 to your computer and use it in GitHub Desktop.
Save kirilkirkov/58c3c5fbd90729088b43 to your computer and use it in GitHub Desktop.
Adding Foreign Key Fails in MySQL (errno 150)
The solution:
The two tables must have the same engine i.e. ENGINE=InnoDB. (can be others: ENGINE=MyISAM works too)
The two tables must have the same charset.
The PK column(s) in the parent table and the FK column(s) must be the same data type. (if the PRIMARY Key in the Parent table is UNSIGNED, be sure to select UNSIGNED in the Child Table field)
The PK column(s) in the parent table and the FK column(s), if they have a define collation type, must have the same collation type;
If there is data already in the foreign key table, the FK column value(s) must match values in the parent table PK columns.
And the child table cannot be a temporary table.
@kirilkirkov
Copy link
Author

Error: Cannot add or update a child row: a foreign key constraint fails
is returned when in one table dont have column number for other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment