【发布时间】:2014-04-18 13:17:15
【问题描述】:
我有一个问题,即使我有ON DELETE CASCADE,当我的父行被删除时,我的子行也没有被删除。
我的父表如下所示:
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
我的子表如下所示:
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
外键“安装”在rw_profile_access 上没有问题,但是当我从rw_profiles 中删除一行时,profile_access 中的相应行(带有rw_profiles.id=rw_profile_access.profile_id)不会自行删除。
为什么我删除父行时子行不删除自己?
如果我运行下面的查询,foreign_key_checks 的值为 on。
SHOW Variables WHERE Variable_name='foreign_key_checks'
【问题讨论】:
-
客户端是否有可能在会话中禁用了foreign_key_checks?
-
它无法在 PHPMyAdmin 和使用我的网站中工作。我已经重新启动了 mysql 服务器,但没有帮助。
-
完全重新安装
mariadb-server也无济于事......
标签: mysql foreign-keys