Fixing SQL Errno: 150

Posted on the 08 January 2013 by Akahgy

Description:

On table altering, SQL or phpMyAdmin throws an 150 error, with a message like Error on rename of ‘./tables/#sql-efc_1′ to ‘./tables/my_table’ (errno: 150)” or “Cannot add or update a child row: a foreign key constraint fails”.

Solution:

Error 150 is related to foreign key relations. Receiving this error means that a foreign key was defined in the table to be altered and it cannot be dropped as its properties are still valid.

What to do:

In phpMyAdmin, go to the table Structure and scroll down to find the “Relation view” link. Click on it and eliminate all the relations given to the table column you want to alter/drop.

In plain MySQL , just drop the foreign key definition, as follows:

ALTER TABLE my_table
DROP FOREIGN KEY fk_table_id;

In Oracle, or other query languages the constraint has to be eliminated :

ALTER TABLE my_table
DROP CONSTRAINT fk_table_id;

After the foreign key definition is eliminated, altering the table should work with no issues.


Tagged: error, mysql, phpmyadmin, sql