I am not able to delete a row from a database object. In my case db object is having self indexing.
A field root_id is having a foreign key relation with key_id (Primary key). The referential integrity is designed as ON UPDATE NO ACTION AND ON DELETE NO ACTION. Even still I am not able to delete.
CONSTRAINT `fk_MyTab` FOREIGN KEY (`root_id`) REFERENCES `MyTab` (`key_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I have to delete records from this table WHERE key_id = root_id.
When I try to delete I am getting the following.
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`MyTab`, CONSTRAINT `fk_MyTab` FOREIGN KEY (`root_id`) REFERENCES `MyTab` (`key_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Point me to the right direction.
That is what
NO ACTIONdoes, it prevents deletion of a row with any child rows. If you want to delete the children too, useCASCADE.You could also preserve the data by setting it to
ON DELETE SET NULL, in which case if the parent is deleted the child rows will have their foreign key column set to null.