Question

I have a problem where my child row is not deleted when my parent row is deleted even tho I have ON DELETE CASCADE.

My parent table looks like this:

CREATE TABLE `rw_profiles` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 ...
 PRIMARY KEY (`id`),
 KEY `profiles_logo_id_foreign` (`logo_id`),
 KEY `profiles_subscription_id_foreign` (`subscription_id`),
 KEY `profiles_deleted_at_name_index` (`deleted_at`,`name`),
 CONSTRAINT `profiles_logo_id_foreign` FOREIGN KEY (`logo_id`) REFERENCES `rw_profile_logos` (`id`),
 CONSTRAINT `profiles_subscription_id_foreign` FOREIGN KEY (`subscription_id`) REFERENCES `rw_subscription_types` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci

My child table looks like this:

CREATE TABLE `rw_profile_access` (
 `profile_id` int(10) unsigned NOT NULL,
 `user_id` int(10) unsigned NOT NULL,
 ...
 UNIQUE KEY `profile_access_profile_id_user_id_unique` (`profile_id`,`user_id`),
 KEY `profile_access_user_id_foreign` (`user_id`),
 CONSTRAINT `profile_access_profile_id_foreign` FOREIGN KEY (`profile_id`) REFERENCES `rw_profiles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `profile_access_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `rw_users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci

The foreign key is "installed" on rw_profile_access without problem, but when I delete a row from rw_profiles, the corresponding row (with rw_profiles.id=rw_profile_access.profile_id) from profile_access doesn't delete itself.

Why doesn't the child row delete itself when I delete a parent row?

The foreign_key_checks value is on if I run the query below.

SHOW Variables WHERE Variable_name='foreign_key_checks'
Was it helpful?

Solution

Must've been something wrong with mariadb-server (version 10.0.10), I remove it and is now using mysql-server instead, now it works like it should.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top