Bogus foreign key constraint fail

MysqlInnodb

Mysql Problem Overview


I get this error message:

> ERROR 1217 (23000) at line 40: Cannot > delete or update a parent row: a > foreign key constraint fails

... when I try to drop a table:

DROP TABLE IF EXISTS `area`;

... defined like this:

CREATE TABLE `area` (
  `area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
  `nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
  `descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
  PRIMARY KEY (`area_id`),
  UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;

The funny thing is that I already dropped all other tables in the schema that have foreign keys against area. Actually, the database is empty except for the area table.

How can it possibly have child rows if there isn't any other object in the database? As far as I know, InnoDB doesn't allow foreign keys on other schemas, does it?

(I can even run a RENAME TABLE area TO something_else command :-?)

Mysql Solutions


Solution 1 - Mysql

On demand, now as an answer...

When using MySQL Query Browser or phpMyAdmin, it appears that a new connection is opened for each query (http://bugs.mysql.com/bug.php?id=8280">bugs.mysql.com/bug.php?id=8280</a>;), making it neccessary to write all the drop statements in one query, eg.

SET FOREIGN_KEY_CHECKS=0; 
DROP TABLE my_first_table_to_drop; 
DROP TABLE my_second_table_to_drop; 
SET FOREIGN_KEY_CHECKS=1; 

Where the SET FOREIGN_KEY_CHECKS=1 serves as an extra security measure...

Solution 2 - Mysql

Two possibilities:

  1. There is a table within another schema ("database" in mysql terminology) which has a FK reference
  2. The innodb internal data dictionary is out of sync with the mysql one.

You can see which table it was (one of them, anyway) by doing a "SHOW ENGINE INNODB STATUS" after the drop fails.

If it turns out to be the latter case, I'd dump and restore the whole server if you can.

MySQL 5.1 and above will give you the name of the table with the FK in the error message.

Solution 3 - Mysql

Disable foreign key checking

SET FOREIGN_KEY_CHECKS=0

Solution 4 - Mysql

from this blog:

You can temporarily disable foreign key checks:

SET FOREIGN_KEY_CHECKS=0;

Just be sure to restore them once you’re done messing around:

SET FOREIGN_KEY_CHECKS=1;

Solution 5 - Mysql

hopefully its work

SET foreign_key_checks = 0; DROP TABLE table name; SET foreign_key_checks = 1;

Solution 6 - Mysql

On Rails, one can do the following using the rails console:

connection = ActiveRecord::Base.connection
connection.execute("SET FOREIGN_KEY_CHECKS=0;")

Solution 7 - Mysql

Maybe you received an error when working with this table before. You can rename the table and try to remove it again.

ALTER TABLE `area` RENAME TO `area2`;
DROP TABLE IF EXISTS `area2`;

Solution 8 - Mysql

i found an easy solution, export the database, edit it what you want to edit in a text editor, then import it. Done

Solution 9 - Mysql

Cannot delete or update a parent row: a foreign key constraint fails (table1.user_role, CONSTRAINT FK143BF46A8dsfsfds@#5A6BD60 FOREIGN KEY (user_id) REFERENCES user (id))

What i did in two simple steps . first i delete the child row in child table like

mysql> delete from table2 where role_id = 2 && user_id =20;

Query OK, 1 row affected (0.10 sec)

and second step as deleting the parent

delete from table1 where id = 20;

Query OK, 1 row affected (0.12 sec)

By this i solve the Problem which means Delete Child then Delete parent

i Hope You got it. :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question&#193;lvaro Gonz&#225;lezView Question on Stackoverflow
Solution 1 - MysqlKarlis RodeView Answer on Stackoverflow
Solution 2 - MysqlMarkRView Answer on Stackoverflow
Solution 3 - MysqlFlakron BytyqiView Answer on Stackoverflow
Solution 4 - MysqlJackDView Answer on Stackoverflow
Solution 5 - MysqlM_ FaView Answer on Stackoverflow
Solution 6 - MysqlyeyoView Answer on Stackoverflow
Solution 7 - MysqlVadim PluzhinskyView Answer on Stackoverflow
Solution 8 - MysqlAbdulrahman KView Answer on Stackoverflow
Solution 9 - MysqlAadil MasavirView Answer on Stackoverflow