After doing some research, it would seem that errno 150 occurs when either table engine types don’t match, or when foreign key creation fails for whatever reason. Anyway, my Create script looks like this:
CREATE TABLE IF NOT EXISTS Users
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
fooType INT UNSIGNED NOT NULL DEFAULT 0,
...More columns, nothing special...
PRIMARY KEY (`id`),
CONSTRAINT Users_fooType_fk
FOREIGN KEY (fooType)
REFERENCES FooTypes (id)
ON DELETE CASCADE
) ENGINE = InnoDB#
CREATE TABLE IF NOT EXISTS FooTypes
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
...More columns, nothing special...
PRIMARY KEY (`id`)
) ENGINE = InnoDB#
So, my table engines match both explicitly and implicitly (I’m using MySQL 5.1.49, which uses InnoDB by default AFAIK).
So the only difference is that Users.fooType has a default. Is that the problem? If it is, how can I set up a default? If it isn’t, anybody see anything else wrong with the code? Sorry I had to obfuscate it slightly, but you’ll have to take my word for it that the obfuscated chunks of the code are correct/not related.
You have to create table
FooTypesbeforeUsers, as it uses the reference ofFooTypeseven beforeFooTypesexist, means violation of Foreign Key constraint, that is,FooTypes(id)must be exist before providingUsers(fooType)its reference.