I have to create a database with two tables in MySQL, but the script fails with errno 150 (foreign key problem). I double-checked the foreign key fields to be the same on bo
In my case I got old table definition MyISAM in one of the tables and obviously I was unable to make foreign key to it from another table. Maybe this help someone.
So this may happen because of inconsistencies between two databases/fields definitions try to check:
Field Type
Field Collation
Table Engine
If nothing works, try this:
The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this.
I was using MySQL workBench
. THe issue is you cannot use the same foreign key name
, they need to be unique
. So if more than one table will reference the same foreign key, each time there must be a unique
name given.
If you are working on mysql workbench and you get this error for a relationship table there may be a quick fix for you: just delete it and let mysql workbench recreate it for you. Then copy the sql. Fixed my errno 150 problem.