Friday, August 16, 2013

Why won't MySQL Workbench let me add a Foreign Key relationship?

You might be unable to check the desired foreign column box in MySQL Workbench.

If you are trying to add the foreign key on the command line, then you may also be getting "a can't create table 150 alter table" and Error Code 1005 error.

Make sure that you have the table and column names correct.

Also make sure that the two columns are EXACTLY the same type.  Check for int(10) vs int(11) and int(10) vs int (10) unsigned.  They have to be EXACTLY the same type.  MySQL Workbench may not show or let you change int(10) to int(10) unsigned, but you can check the exact column type using "show create table", and you can change it on the command line like this:

ALTER TABLE TableName CHANGE columnName columnName  int(10) unsigned DEFAULT NULL

After this, you should be able to check the desired box in MySQL Workbench.

Also, make sure that both tables are using InnoDB

Also see: http://stackoverflow.com/questions/9018584/error-code-1005-cant-create-table-errno-150

No comments: