简体   繁体   English

删除级联上的外部约束不起作用

[英]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.

相关问题 MySQL外部约束在DELETE CASCADE上 - MySQL Foreign Constraint ON DELETE CASCADE 删除级联上更新级联的外键约束 - foreign key constraint on update cascade on delete cascade 外键约束On删除级联无效 - Foreign Key Constraint On delete cascade not effective 使用SQLAlchemy删除级联外键约束错误 - Delete cascade foreign key constraint error with SQLAlchemy 外键选项,ON DELETE CASCADE不起作用? - The Foreign Key Options, ON DELETE CASCADE not working? MariaDB / MySQL外键约束:在删除时是否可以请求级联? - MariaDB/MySQL foreign key constraint: possible to request cascade at time of delete? 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) 删除外键级联 - Delete cascade for foreign keys 如何将 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外键级联删除不起作用,完全陷入困境 - MySQL foreign key cascade delete not working, completely stumped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM