SQLSTATE[HY000]: General error: 1005 Can't create table '*' (errno: 150)
You may or may have not already stumbled upon this rather obscure error message (no, I'm not starting a serie on obscures error messages). Well the first thing to think when you encounter this is your foreign keys. This message generally denotes a failure in foreign keys creation, for example when the two columns you are trying to link are not of the exact same type (that is, you can't link an integer(4) to an integer(11)).
Now what if you are absolutely certain that your two columns are of the same type ? I just had the problem with this piece of doctrine schema (simplified for example purpose):
Media:
columns:
image: { type: string(255), notnull: true, unique: true }
ExplorationMission:
columns:
probe_image: { type: integer, notnull: true }
relations:
ProbeImage: { class: Media, local: probe_image, foreign: id, onDelete: 'SET NULL' }
Can you spot the problem ?
It's rather obvious when you know it, but I spent rather some time cursing mysql in vain. The point is that there is a major logic error in this schema. it declares the probe_image as NOT NULL while it gently asks the FK to set it null on delete. See ?
Hope it saves some frustration :-)
Comments
Thanks! Saved me a lot of time. I had to delete the old tables, too. After that everything worked fine.
This issue most commonly occurs, I think, because the referenced column is not a primary key.
Thanks for the tip! I'm using Doctrine ORM and use the auto identifier feature. But when writing Many-To-Many classes you have to explicitly declare the length and type of the foreign keys and they did not match the defaults so i changed it appropriately and now it works! Woho! Txn ;-)