tags:

views:

366

answers:

2

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 :-?)

+2  A: 

Disable foreign key checking

DISABLE KEYS
Flakron Bytyqi
The correct command appears to be `SET FOREIGN_KEY_CHECKS=0` and it does fix the error message. Do you have any idea about why this is required? Are foreign keys cached even after the tables are gone?
Álvaro G. Vicario
Well to say the truth, I have no idea why such a problem arises, but make sure you disable key checking every time you make some huge changes or updates. It has happened to me several times, leaving me without sleep for days.
Flakron Bytyqi
+1  A: 

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.

MarkR
I can no longer reproduce the issue. The out of sync dictionary stands out as a likely reason. I'll test it day and see what `SHOW ENGINE INNODB STATUS` reports.
Álvaro G. Vicario