ERROR 1215(HY000)at line:无法添加外键约束

时间:2018-05-04 15:00:13

标签: mysql

我在第966行遇到“错误1215(HY000):无法添加外键约束”我尝试导入数据库转储时出错。这是第966行:

CREATE TABLE `CATEGORIE_tags` (
      `categorie_id` int(11) NOT NULL,
      `tag_id` int(11) NOT NULL,
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `flag` tinyint(1) NOT NULL DEFAULT '0',
      `tags_id` bigint(20) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `tag_id` (`tag_id`),
      KEY `categorie_id` (`categorie_id`),
      KEY `FKACB212415C86F159` (`categorie_id`),
      KEY `FKACB21241925C59A` (`tag_id`),
      CONSTRAINT `CATEGORIE_tags_ibfk_1` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`),
      CONSTRAINT `CATEGORIE_tags_ibfk_2` FOREIGN KEY (`categorie_id`) REFERENCES `CATEGORIE` (`CAT_ID`),
      CONSTRAINT `FKACB212415C86F159` FOREIGN KEY (`categorie_id`) REFERENCES `CATEGORIE` (`CAT_ID`),
      CONSTRAINT `FKACB21241925C59A` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1331 DEFAULT CHARSET=utf8;
    /*!40101 SET character_set_client = @saved_cs_client */;
你知道为什么? 感谢

1 个答案:

答案 0 :(得分:1)

如果以错误的顺序导入表格,则约束检查将失败 示例:您在tags表之前创建Category_tags表,而Category_tags引用标签。

修复:导入时禁用外键检查。

SET FOREIGN_KEY_CHECKS = 0;

--  / import dump code here / 

SET FOREIGN_KEY_CHECKS = 1;  

http://www.stetsenko.net/2008/10/mysql-how-to-ignore-checking-of-foreign-key-constraints-for-innodb-tables/