MySql Encountering Error 150: Deciphering Foreign Key Woes
When attempting to create a table with a foreign key referencing another table, developers may encounter error 150, baffling them with its cryptic message. This article delves into the underlying cause of this error and provides a solution.
As detailed in the MySQL documentation, error 150 occurs when recreating a table that was previously dropped does not adhere to the foreign key constraints referencing it. Specifically, the table must maintain the same column names, types, and indexes on the referenced keys as when it was originally created.
To resolve this issue, ensure that the table containing the foreign key (in this case, "foo") is also created as an InnoDB table. As per the MySQL documentation, both tables involved in a foreign key relationship must be InnoDB tables and not temporary tables.
Therefore, to correct the error, try recreating the "foo" table as an InnoDB table using the following query:
CREATE TABLE foo ( id INT PRIMARY KEY ) ENGINE = InnoDB;
Once the "foo" table is created as an InnoDB table, you should be able to successfully create the "bar" table with the foreign key reference without encountering error 150.
The above is the detailed content of MySQL Error 150: Why Can\'t I Create This Foreign Key?. For more information, please follow other related articles on the PHP Chinese website!