【发布时间】:2015-04-29 22:50:08
【问题描述】:
这个 sql 来自 mysqldump 文件,它一直给我:Error Code: 1215. Cannot add foreign key constraint
我为这个不一致挣扎了一个下午,仍然不知道问题到底是什么。
DROP TABLE IF EXISTS `regions`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `regions` (
`region_id` bigint(20) unsigned AUTO_INCREMENT,
`region_code` int(6) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`parent_code` int(6) unsigned DEFAULT NULL,
`type` varchar(255) NOT NULL,
PRIMARY KEY (`region_id`),
UNIQUE KEY `ui_RegionCode` (`region_code`),
CONSTRAINT `regions_ParentCode_RegionCode` FOREIGN KEY (`parent_code`) REFERENCES `regions` (`region_code`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3525 DEFAULT CHARSET=utf8;
【问题讨论】:
-
这是什么 MySQL?使用 5.5.9 我没有问题。
-
我在服务器上得到了 5.5.40-MariaDB,在我的本地机器上得到了 5.6.20。
-
我在 staging 的 mysql 转储中遇到了同样的问题。据我所知,这些表具有正确的约束;最后通过删除
DEFAULT CHARSET=utf8mb4的所有实例来解决。不是为什么为什么会得到“1215。无法添加外键约束”。 -
我在 MySQL 5.7.8rc 上遇到了同样的错误,但删除了所有默认字符集,因为 @Ithar 说帮助我。谢谢
标签: mysql sql debugging datatable