【问题标题】:Foreign row not deleted (have cascade), why?外行没有被删除(有级联),为什么?
【发布时间】: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


【解决方案1】:

mariadb-server(版本 10.0.10)一定有问题,我删除了它,现在改用mysql-server,现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 2012-09-22
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2011-04-20
    • 2019-04-26
    • 2015-12-10
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多