【发布时间】:2016-07-12 06:18:24
【问题描述】:
在 phpmyadmin 中创建 2 个表时,出现这样的错误。
MySQL 说:文档
#1215 - 无法添加外键约束
我的表结构是
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_store 表
【问题讨论】:
-
我已经成功创建了这些表,但没有对 iwd_storelocator_store 进行 FK。也许这张表是问题的原因?
-
iwd_storelocator_store表的结构在哪里? -
我的 iwd_storelocator_store 表结构已给出。
-
帕维尔..你是对的。我也可以在没有 FK 的情况下创建 iwd_storelocator_store 。但我在第二张桌子上也需要那个 id
-
编辑了我的答案@Sheen
标签: mysql phpmyadmin