MySQL Foreign Key Error 1005 errno 150 primary key as foreign key

When creating a foreign key constraint, MySQL requires a usable index on both the referencing table and also on the referenced table. The index on the referencing table is created automatically if one doesn’t exist, but the one on the referenced table needs to be created manually (Source). Yours appears to be missing. Test case: … Read more

MySQL. Can’t create table errno 150

table1.field1 has no index defined on it. It is required to place a FOREIGN KEY constraint on field1. With this: CREATE TABLE IF NOT EXISTS `testdb`.`table1` ( `id` INT UNSIGNED NOT NULL , `field1` VARCHAR(50) NULL , KEY ix_table1_field1 (field1), PRIMARY KEY (`id`) ) ENGINE = InnoDB; Everything should then work as expected.