Home:ALL Converter>MySql Error 150 - Foreign keys

MySql Error 150 - Foreign keys

Ask Time:2009-05-05T23:14:50         Author:Martin Thurau

Json Formatter

When I execute the follow two queries (I have stripped them down to absolutely necessary):

mysql> CREATE TABLE foo(id INT PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE bar ( id INT, ref INT, FOREIGN KEY (ref) REFERENCES foo(id)) ENGINE InnoDB;

I get the following error: ERROR 1005 (HY000): Can't create table './test/bar.frm' (errno: 150)

Where the **** is my error? I haven't found him while staring at this for half an hour.

Author:Martin Thurau,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/825362/mysql-error-150-foreign-keys
Greg :

From FOREIGN KEY Constraints\n\n\n If you re-create a table that was\n dropped, it must have a definition\n that conforms to the foreign key\n constraints referencing it. It must\n have the right column names and types,\n and it must have indexes on the\n referenced keys, as stated earlier. If\n these are not satisfied, MySQL returns\n error number 1005 and refers to error\n 150 in the error message.\n\n\nMy suspicion is that it's because you didn't create foo as InnoDB, as everything else looks OK.\n\nEdit: from the same page -\n\n\n Both tables must be InnoDB tables and they must not be TEMPORARY tables. \n",
2009-05-05T15:22:11
user2906840 :

You can use the command SHOW ENGINE INNODB STATUS to get more specific information about the error.\n\nIt will give you a result with a Status column containing a lot of text.\n\nLook for the section called LATEST FOREIGN KEY ERROR which could for example look like this:\n\n------------------------\nLATEST FOREIGN KEY ERROR\n------------------------\n190215 11:51:26 Error in foreign key constraint of table `mydb1`.`contacts`:\nCreate table `mydb1`.`contacts` with foreign key constraint failed. You have defined a SET NULL condition but column 'domain_id' is defined as NOT NULL in ' FOREIGN KEY (domain_id) REFERENCES domains (id) ON DELETE SET NULL ON UPDATE CASCADE,\n CONSTRAINT contacts_teams_id_fk FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE SET NULL ON UPDATE CASCADE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT' near ' ON DELETE SET NULL ON UPDATE CASCADE,\n CONSTRAINT contacts_teams_id_fk FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE SET NULL ON UPDATE CASCADE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT'.\n",
2013-10-22T14:37:27
Suyash Jain :

To create a foreign key , \n\n\nboth the main column and the reference column must have same definition.\nboth tables engine must be InnoDB.\n\n\nYou can alter the engine of table using this command , please take the backup before executing this command.\n\nalter table [table name] ENGINE=InnoDB;",
2014-01-14T10:49:10
webmaster :

I had the same problem, for those who are having this also:\n\ncheck the table name of the referenced table\n\nI had forgotten the 's' at the end of my table name\n\neg table Client --> Clients\n\n:)",
2013-01-21T18:51:43
Madhab Chandra Pal :

Apart form many other reasons to end up with MySql Error 150 (while using InnoDB), One of the probable reason, is the undefined KEY in the create statement of the table containing the column name referenced as a foreign key in the relative table.\n\nLet's say the create statement of master table is -\n\nCREATE TABLE 'master_table' (\n 'id' int(10) NOT NULL AUTO_INCREMENT,\n 'record_id' char(10) NOT NULL,\n 'name' varchar(50) NOT NULL DEFAULT '',\n 'address' varchar(200) NOT NULL DEFAULT '',\n PRIMARY KEY ('id')\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\nand the create syntax for the relative_table table where the foreign key constraint is set from primary table -\n\nCREATE TABLE 'relative_table' (\n 'id' int(10) NOT NULL AUTO_INCREMENT,\n 'salary' int(10) NOT NULL DEFAULT '',\n 'grade' char(2) NOT NULL DEFAULT '',\n 'record_id' char(10) DEFAULT NULL,\n PRIMARY KEY ('id'),\n CONSTRAINT 'fk_slave_master' FOREIGN KEY ('record_id') REFERENCES 'master' ('record_id')\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\nThis script is definitely going to end with MySql Error 150 if using InnoDB.\n\nTo solve this, we need to add a KEY for the The column record_id in the master_table table and then reference in the relative_table table to be used as a foreign_key.\n\nFinally, the create statement for the master_table, will be - \n\nCREATE TABLE 'master_table' (\n 'id' int(10) NOT NULL AUTO_INCREMENT,\n 'record_id' char(10) NOT NULL,\n 'name' varchar(50) NOT NULL DEFAULT '',\n 'address' varchar(200) NOT NULL DEFAULT '',\n PRIMARY KEY ('id'),\n KEY 'record_id' ('record_id')\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
2013-11-21T10:15:48
Hamed J.I :

I had very same problem and the reason was the \"collation\" of columns was different. One was latin1 while the other was utf8",
2013-08-07T11:01:11
yy