I have problem with cleaning my database up when some row and relation is deleted.
CREATE TABLE IF NOT EXISTS `cms_users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`id_account_type` smallint(2) unsigned NOT NULL,
PRIMARY KEY (`id`)
);
Above are some users. There are children of those users and they are stored in other table:
CREATE TABLE IF NOT EXISTS `cms_users_relations` (
`id_user` int(10) unsigned NOT NULL,
`id_parent` int(10) unsigned DEFAULT NULL,
UNIQUE KEY `id_user` (`id_user`),
KEY `constraint_9` (`id_parent`)
);
ALTER TABLE `cms_users_relations`
ADD CONSTRAINT `constraint_8` FOREIGN KEY (`id_user`) REFERENCES `cms_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `constraint_9` FOREIGN KEY (`id_parent`) REFERENCES `cms_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
The problem is that when I remove parent from cms_users all children should be removed too but they don't. I created a triger for this purpose but it don't do the job:
DELIMITER //
CREATE TRIGGER `delete_user` AFTER DELETE ON `cms_users_relations`
FOR EACH ROW BEGIN
DELETE FROM cms_users WHERE OLD.id_user = cms_users.id;
END
//
DELIMITER ;
It looks like mysql server don't bother that we are deleting the user, then deleting relation by foreign key and then we should put the trigger...
Any ideas?