简体   繁体   English

无法使用CASEADE上的DELETE设置FOREIGN KEY

[英]Can not set FOREIGN KEY with DELETE ON CASCADE

I would like delete referencing data with a FOREIGN KEY. 我想使用FOREIGN KEY删除引用数据。

Here are two of my datatables: 这是我的两个数据表:

CREATE TABLE `specification_variant_parent` (
  `specification_variant_parent_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `sort_order` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`specification_variant_parent_id`)
) ENGINE=InnoDB;


CREATE TABLE `specification_variant_parent_description` (
  `specification_variant_parent_id` smallint(5) unsigned NOT NULL,
  `language_id` tinyint(3) unsigned NOT NULL,
  `name` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`specification_variant_parent_id`,`language_id`),
  KEY (`specification_variant_parent_id`)
  REFERENCES specification_variant_parent (specification_variant_parent_id) ON DELETE CASCADE
) ENGINE=InnoDB;

When I fire it in phpMyAdmin it says: 当我在phpMyAdmin中启动它时,它说:

Error in foreign key constraint of table database/specification_variant_parent_description: FOREIGN KEY ( specification_variant_parent_id ) REFERENCES specification_variant_parent (specification_variant_parent_id) ON DELETE CASCADE ) ENGINE=InnoDB: Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. 表数据库/ specification_variant_parent_description的外键约束中的错误:FOREIGN KEY( specification_variant_parent_id )REFERENCES specification_variant_parent(specification_variant_parent_id)ON DELETE CASCADE)ENGINE = InnoDB:无法在引用表中找到索引,其中引用列作为第一列,或者该表和被引用表中的约束不匹配。

What is wrong with my database scheme? 我的数据库方案有什么问题?

Your datatypes do not match. 您的数据类型不匹配。

In the first table you have 在第一个表中

`specification_variant_parent_id` int(11) unsigned NOT NULL AUTO_INCREMEN

The datatype you declare is an unsigned int 您声明的数据类型是无符号的int

In the second table the foreign key is 在第二个表中,外键是

`specification_variant_parent_id` smallint(5) unsigned NOT NULL

and the datatype is an unsigned smallint 并且数据类型是无符号smallint

Make both of them the same datatype. 使它们都具有相同的数据类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM