简体   繁体   English

如何调试mysql外键约束,ON DELETE CASCADE不能在生产环境中从子表中删除行

[英]How to debug mysql foreign key constraint, ON DELETE CASCADE not deleting rows from Child table on production environment

I have defined 2 tables and a foriegn key constraint between them as follows: 我已经定义了2个表和它们之间的前键约束,如下所示:

| users | CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_master_id` int(11) NOT NULL,
  `user_type_id` int(11) NOT NULL,
  `user_group_id` int(11) NOT NULL,
  `user_type_code` char(1) NOT NULL,
  `membership_number` varchar(40) NOT NULL,
  `password` varchar(60) NOT NULL,
  `email` varchar(200) NOT NULL,
  `isd` varchar(10) NOT NULL,
  `mobile` varchar(20) NOT NULL,
  `passenger_id` int(11) NOT NULL,
  `added_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `added_by` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  KEY `account_master_id` (`account_master_id`),
  CONSTRAINT `acMaster_to_user` FOREIGN KEY (`account_master_id`) REFERENCES `account_master` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=189 DEFAULT CHARSET=utf8 |


user_oauth | CREATE TABLE `user_oauth` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `service` varchar(30) NOT NULL,
  `auth_id` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `auto_share` tinyint(4) NOT NULL,
  `photo` varchar(255) NOT NULL,
  `auth_token_short` varchar(255) DEFAULT NULL,
  `auth_details` text NOT NULL,
  `device_type` varchar(60) NOT NULL,
  `login_date` datetime NOT NULL,
  `login_ip` varchar(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user` (`user_id`),
  CONSTRAINT `user_to_oauth` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=109 DEFAULT CHARSET=latin1 |

on deleting a row from users table, deletes corresponding entries from user_oauth table in LOCAL and Staging environment. 在从用户表删除行时,在本地和登台环境中从user_oauth表中删除相应的条目。 but however same thing is not working in PRODUCTION environment. 但是,在PRODUCTION环境中,同样的事情不起作用。 I want to know how can i debug this. 我想知道我该如何调试。

update: 更新:

  • both tables are innodb 两个表都是innodb
  • running mysql in strict mode 在严格模式下运行mysql

The reasons why foreign keys might not work are: 外键可能不起作用的原因是:

  • they are not supported (eg on MyISAM-tables) 不支持它们(例如,在MyISAM表上)
  • they are disabled 他们被禁用

MySQL allows you to disable foreign key contraints by setting the system variable foreign_key_checks to 0 . MySQL允许您通过将系统变量foreign_key_checks设置为0来禁用外键约束 This will allow you to violate all foreign key constraints (eg delete a parent or add parentless childs). 这将允许您违反所有外键约束(例如,删除父项或添加无父项的子项)。 But it will in turn also disable related features like cascades, which are there to automatically prevent specific constraint violations (by eg deleting the children) - which of course won't occur anymore if the option is disabled. 但是,这反过来又会禁用相关的功能,例如级联,这些功能可以自动防止特定的约束冲突(例如,通过删除子级)-如果禁用该选项,当然不会再发生这种情况。

The idea is to help you with some administration tasks, eg importing data when the referenced data is not yet there, but should usually not be used during normal operation. 这个想法是为了帮助您完成一些管理任务,例如,当引用的数据尚不存在时导入数据,但通常不应在正常操作期间使用它。 If the setting reappears, you might want to check your apps if one is setting this option by accident, as it is enabled after every server start by default and has to be disabled explicitely. 如果再次出现该设置,则可能要检查您的应用程序是否偶然设置了此选项,因为默认情况下,每台服务器启动后就会启用该选项,并且必须明确禁用它。

You can check the current setting by eg using 您可以通过以下方式检查当前设置:

select @@foreign_key_checks;

You can use 您可以使用

SET foreign_key_checks = 1;

to enable it again, but be aware that it will not check your current data: 再次启用它,但是请注意它将不会检查您的当前数据:

Setting foreign_key_checks to 1 does not trigger a scan of the existing table data. 将foreign_key_checks设置为1不会触发对现有表数据的扫描。 Therefore, rows added to the table while foreign_key_checks=0 will not be verified for consistency. 因此,将不会验证foreign_key_checks = 0时添加到表中的行的一致性。

So you will have to check and fix it yourself. 因此,您必须自己检查并修复它。 You can do it either before or after you enabled the setting again, although it might be easier to do it before. 您可以在启用该设置之前或之后进行此操作,尽管之前进行设置可能会更容易。 To trigger a recheck, you can and should drop and recreate the foreign key, just to make sure everything is consistent now. 要触发重新检查,您可以并且应该删除并重新创建外键,只是为了确保现在一切都一致。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 mysql 8 中的约束 FOREIGN KEY 从 laravel 迁移或原始 ZAC5C74B64B4B8352EF2F181AFFB5AC 中的“ON DELETE CASCADE”更改为“ON DELETE SET NULL” - How to change constraint FOREIGN KEY in mysql 8 from `ON DELETE CASCADE` to `ON DELETE SET NULL` in laravel migration or raw sql MySQL #1452 - 无法添加或更新子行:外键约束失败 ON DELETE CASCADE ON UPDATE CASCADE) - MySQL #1452 - Cannot add or update a child row: a foreign key constraint fails ON DELETE CASCADE ON UPDATE CASCADE) 如何在mysql表的外键中正确地级联删除? - How to cascade on delete in a foreign key of a mysql table correctly? MySQL外部约束在DELETE CASCADE上 - MySQL Foreign Constraint ON DELETE CASCADE 如果从子表中删除行,则“在删除级联上” - “On Delete Cascade” if deleting a row from the child table 删除级联上更新级联的外键约束 - foreign key constraint on update cascade on delete cascade MariaDB / MySQL外键约束:在删除时是否可以请求级联? - MariaDB/MySQL foreign key constraint: possible to request cascade at time of delete? 当父级被删除时,Mysql“级联删除”不会在外键行上删除 - Mysql 'Cascade On Delete' Not Deleting on Foreign Key Row When Parent Is Deleted 从表中删除行:“无法删除或更新父行:外键约束失败”是外键问题或CASCADE会完成这项工作吗? - Delete row from table:“Cannot delete or update a parent row:a foreign key constraint fails” is that a FOREIGN KEY problemm or CASCADE will do the job? 在具有外键约束的表中添加行[MySQL] - Adding rows to table with foreign key constraint [MySQL]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM