Tell me why I can not delete a record from the Departments table?

Organizations ---------------- id - PK Departments ---------------- id org_id - FK Cannot delete or update a parent row: a foreign key constraint fails (`emedia_backend`.`users`, CONSTRAINT `FK_dep_id` FOREIGN KEY (`dep_id`) REFERENCES `departments` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) 

    1 answer 1

    Judging by the text of the error

     Cannot delete or update a parent row: a foreign key constraint fails (`emedia_backend`.`users`, CONSTRAINT `FK_dep_id` FOREIGN KEY (`dep_id`) REFERENCES `departments` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) 

    You have an unlisted table users in the question and it has an entry with the department being removed. First, delete users or set another dep_id for them or make it NULL.

    You can also change your foreign key and give it a default on delete cascade action to automatically delete records from the users table when removing the departments with which they are associated or on delete set null to set their dep_id to NULL.

    • Not exactly noticed (Thank you - quaresma89
    • Well, I know about the cascade, in this situation I do not need to delete records from the child table, but the second option will do - quaresma89