Bug #15746 | Foreign Keys: Self-referential constraints incorrectly prevent deletes | ||
---|---|---|---|
Submitted: | 14 Dec 2005 16:37 | Modified: | 1 Oct 2009 22:52 |
Reporter: | David Moore | Email Updates: | |
Status: | Duplicate | Impact on me: | |
Category: | MySQL Server: DML | Severity: | S4 (Feature request) |
Version: | 5.0.15 | OS: | Windows (Windows XP) |
Assigned to: | Assigned Account | CPU Architecture: | Any |
[14 Dec 2005 16:37]
David Moore
[16 Dec 2005 14:41]
Valeriy Kravchuk
Thank you for a problem report. Yes, everything works just as you described: mysql> DELETE FROM TRS_COST_CENTREX WHERE COST_CENTRE = 'XXX'; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test/TRS_COST_CENTREX`, CONSTRAINT `COST_CENTREX_11` FOREIGN KEY (`COST_CENTRE_01`) REFERENCES `TRS_COST_CENTREX` (`COST_CENTRE`)) But it looks like a documented feature (http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html) to me: "RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as omitting the ON DELETE or ON UPDATE clause. (Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION and RESTRICT are the same.)" So, that is what you explicitely asked for. Try CASCADE, if you want to allow such operations.
[16 Dec 2005 16:33]
David Moore
Thanks for pointing out where this is covered in the Manual, as obviously I hadn't spotted this. However, I think the more relevant section is(last sentence): "Deviation from SQL standards: Like MySQL in general, in an SQL statement that inserts, deletes, or updates many rows, InnoDB checks UNIQUE and FOREIGN KEY constraints row-by-row. According to the SQL standard, the default behavior should be deferred checking, that is, constraints are only checked after the entire SQL statement has been processed. Until InnoDB implements deferred constraint checking, some things will be impossible, such as deleting a record that refers to itself via a foreign key." Your suggestion of using CASCADE would not be suitable as I am trying to prevent deletes/updates leaving orphan rows. I will implement the constraint in business logic instead. Incidently, Oracle and SQL Server do allow deleting a record that refers to itself via a foreign key.
[17 Dec 2005 13:01]
Valeriy Kravchuk
So, as your particular case is documented, can we make this report a (verified, and useful) feature request? For developers to get yet another reason to implement the standard behaviour? I believe, they are already doing it, but not for 5.0.x...
[19 Dec 2005 10:20]
David Moore
As this is already known behaviour, I'm quite happy to make this a feature request instead of a bug. Best of luck!
[1 Oct 2009 22:52]
Peter Gulutzan
This is a duplicate of Bug#7412 Cannot delete a row that references itself.