[英]Foreign constraint On Delete Cascade not working
I have a custom table created which has a foreign constraint on the core_website
table. 我创建了一个自定义表,该表对
core_website
表具有外部约束。 However, the on delete cascade
isn't working. 但是,
on delete cascade
不起作用。
I did a search and found this relevant thread , which notes that the data types between the two columns have to be the same. 我进行了搜索,发现了这个相关的线程 ,该线程指出两列之间的数据类型必须相同。 Both data types are
smallint(5)
. 两种数据类型均为
smallint(5)
。
I did notice one minor discrepancy in the column definition, which is that in core_website, Allow Null is not set, and Default is not set to zero, whereas in the account table, Allow Null is set and Default is zero. 我确实注意到列定义中的一个细微差异,即在core_website中, 未设置Allow Null,并且Default未设置为零,而在帐户表中,Allow Null 被设置并且Default为零。 I didn't think changing these would have any effect, but I went ahead and changed them on the account table to match, but that didn't help.
我认为更改这些更改不会产生任何效果,但是我继续进行了更改,以便在帐户表上进行更改以使其匹配,但这并没有帮助。
CREATE TABLE `account` (
`account_id` smallint(11) unsigned NOT NULL AUTO_INCREMENT,
`website_id` smallint(5) unsigned DEFAULT '0',
`code` varchar(64) NOT NULL DEFAULT '',
PRIMARY KEY (`account_id`),
UNIQUE KEY `code` (`code`),
KEY `FK_WEBSITE_ID` (`website_id`),
CONSTRAINT `FK_WEBSITE_ID` FOREIGN KEY (`website_id`) REFERENCES `core_website` (`website_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8
try this and let us the result pls. 试试这个,让我们结果请。
CREATE TABLE `account` (
`account_id` smallint(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`website_id` smallint(5) unsigned DEFAULT '0',
`code` varchar(64) NOT NULL DEFAULT '',
UNIQUE KEY `code` (`code`),
KEY `FK_WEBSITE_ID` (`website_id`),
CONSTRAINT `FK_WEBSITE_ID` FOREIGN KEY (`website_id`) REFERENCES `core_website` (`website_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8
You need add foreign key to core_website
table and point it to website_id
field in account
table. 您需要将外键添加到
core_website
表并将其指向account
表中的website_id
字段。 In other words, foreign key should be added to the dependent table and pointed to main table, in this case when you delete row from main table - row from dependent table will be deleted because of FK. 换句话说,应将外键添加到从属表中并指向主表,在这种情况下,当您从主表中删除行时,由于FK,从属表中的行将被删除。 In your case you did it "upside down".
在您的情况下,您将其“颠倒”了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.