无法在 phpmyadmin 中添加外键约束

cannot add foreign key constraint in phpmyadmin

在 phpmyadmin 中创建 2 table 时,我收到这样的错误。

MySQL 说:文档

#1215 - Cannot add foreign key constraint

我的table结构是

CREATE TABLE `iwd_storelocator_manufacturer` (
`entity_id`  int(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name`  varchar(255) NOT NULL ,
`code`  varchar(255) NOT NULL ,
`grayscale_image`  varchar(255) NULL ,
`color_image`  varchar(255) NULL ,
PRIMARY KEY (`entity_id`)
);

CREATE TABLE `iwd_storelocator_manufacturer_to_store` (
`manufacturer_id`  int(11) UNSIGNED NOT NULL ,
`store_id`  int(11) NOT NULL ,
`preferred`  int NULL ,
PRIMARY KEY (`manufacturer_id`, `store_id`),
FOREIGN KEY (`store_id`) REFERENCES `iwd_storelocator_store` (`store_id`) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (`manufacturer_id`) REFERENCES `iwd_storelocator_manufacturer` (`entity_id`) ON DELETE RESTRICT ON UPDATE CASCADE
);

你能告诉我它有什么问题吗?

这是我的iwd_storelocator_storetable

iwd_storelocator_store

为了准确知道问题出在哪里,您必须查看 LATEST FOREIGN KEY ERROR 部分。

使用这个查询来找出答案:

SHOW ENGINE INNODB STATUS

此外,请确保所有数据类型都相同:子列的数据类型必须与父列的数据类型相匹配。

如果问题是 table 的创建顺序(这可能会导致此错误),只需 运行 set foreign_key_checks=0 这样您就可以创建 table s 以任何顺序排列,而不必创建所有父 tables BEFORE 子 tables.

最后,确保所有 table 的编码都相同。

编辑:在你的情况下,你还应该给我们 iwd_storelocator_store table

的结构

现在我们有了您的 iwd_storelocator_store table,我认为您应该在 store_id 列上创建一个索引,因为它不是table