SQLSTATE[HY000]: General error: 1005 Can't create table '*' (errno: 150)
By Geoffrey on Thursday 2 Apr 2009, 15:18 - Geekeries - Permalink
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 ;-)
Thx A lot!!
Thanks a lot! You saved my time ;-)
Nonetheless your example confused me a bit.
Shouldn't the foreign key be 'image' instead of id:
"ProbeImage: { class: Media, local: probe_image, foreign: image, onDelete: 'SET NULL' }"
Something you might be interested in, that took me quarter an hour. integer(11) in schema.yml does not result in int(11) in MySQL, but instead you have to write integer(4) in your schema.yml.
integer integer int/serial
integer(1) tinyint smallint/serial
integer(2) smallint smallint/serial
integer(3) mediumint int/serial
integer(4) int int/serial
integer(5) bigint bigint/bigserial
However, in my case integer without a number resulted in BIGINT.
Thank you mate, you saved my life!!!
Cheers!
\o\ /o/ \o/
Thanks ! =)