views:

307

answers:

4

My table definition:

CREATE TABLE x (
    a INT NOT NULL,
    FOREIGN KEY (a) REFERENCES a (id) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE = InnoDB;

which produces the following error:

ERROR 1005 (HY000): Can't create table './abc/x.frm' (errno: 150)

What does this mean?

+1  A: 

Probably that a.id doesn't exist.

Byron Whitlock
Just figured it out. Table a hadn't been created yet...
+1  A: 

use perror with the "errno" error number to get the error message (perror 150):

MySQL error code 150: Foreign key constraint is incorrectly formed

nathan
+1  A: 

Maybe this is why

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

from: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

Galen
A: 

This also happens if there are duplicate foreign key names.

Say for example that you have two very similar tables with long names. To "save" time, you copy and paste the foreign key names from the first table's CREATE, then get distracted and fail to update the part of the table name that is different. They happen to share a foreign key to another table, resulting in one or more identical foreign key names.

Have you noticed that bashing your head against the monitor isn't as satisfying with an LCD?

Ivo Roper