[英]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: 更新:
The reasons why foreign keys might not work are: 外键可能不起作用的原因是:
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.