Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL drop field; foreign key errorno 150

Problems with MySQL using InnoDB and dropping an unused, foreign key. The foreign key references another table's id. However, I don't need this field.

I've tried removing the fk index, which doesn't work - says it's needed in a foreign key contraint. And removing the field, which gives me an error:

1025 - Error on rename of './axis/#sql-ad8_1531' to './axis/Schedule' (errno: 150)

The table is currently empty. There are no tables referencing this field. Any ideas on how to get rid of this? Other than creating a new table?

If I'm reading the below error correctly, I can't drop the column since the fk index is declared. And I can't drop the index, because the column exists. Chicken & Egg??

LATEST FOREIGN KEY ERROR 111004 17:05:40 Error in foreign key constraint of table axis/Schedule: there is no index in the table which would contain the columns as the first columns, or the data types in the table do not match the ones in the referenced table or one of the ON ... SET NULL columns is declared NOT NULL. Constraint: , CONSTRAINT "fk_Schedule_Grp" FOREIGN KEY ("idGrp") REFERENCES "Grp" ("idGrp") ON DELETE NO ACTION ON UPDATE NO ACTION InnoDB: Renaming table axis. to axis.Schedule failed!

like image 749
Logan Klenner Avatar asked Oct 04 '11 23:10

Logan Klenner


People also ask

Can we drop foreign key column?

You can delete a foreign key constraint in SQL Server by using SQL Server Management Studio or Transact-SQL. Deleting a foreign key constraint removes the requirement to enforce referential integrity.

Does Drop Table remove foreign keys?

If you drop the "child" table first, the foreign key will be dropped as well.

Which clause is used to remove the foreign key constraint?

To remove a foreign key constraint, the 'DROP' clause is used.


1 Answers

You need to use

ALTER TABLE table_name DROP FOREIGN KEY constraint_name

Here constraint_name is the name of the constraint rather than the index. If you do not know what this is, you can find out by issuing a SHOW CREATE TABLE. It is the identifier that appears after the word CONSTRAINT.

Edit: From your addition to the question, it looks like you need to issue

ALTER TABLE table_name DROP FOREIGN KEY fk_Schedule_Grp
like image 50
Hammerite Avatar answered Oct 16 '22 08:10

Hammerite