我正在尝试执行以下 SQL 并收到 errno: 150 'cannot create table path_relations' 作为响应。根据 MySQL 文档,这是由我的 FOREIGN KEY 限制有问题引起的。我做错了什么?
DROP TABLE IF EXISTS `paths`;
DROP TABLE IF EXISTS `path_relations`;
CREATE TABLE `paths` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(256) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `path_relations` (
`ancestor` int(11) NOT NULL DEFAULT '0',
`descendant` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY(`ancestor`, `descendant`),
FOREIGN KEY(`ancestor`) REFERENCES paths(`id`),
FOREIGN KEY(`descendant`) REFERENCES paths(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
最佳答案
这是给你的 list ,祝你好运;)
1) 外键源类型和引用字段必须相同
2) 来源和引用字段都必须是无符号的
3) 源字段必须被索引
4) 两个表都必须是InnoDB
关于mysql - 将带有 FOREIGN KEY 的表添加到 MySQL 数据库会导致 errno 150,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11414685/