【发布时间】:2019-10-23 03:44:50
【问题描述】:
我在 MariaDB 中创建多对多关系表时遇到问题。 我尝试过使用工作台、手动创建脚本和“显示创建表 xxxxxxx;”从另一个已经创建的 n 到 n 表但结果始终相同,以下错误:
Error Code: 1005. Can't create table `asi_234_api_establecimientos`.`oe_modalidad` (errno: 150 "Foreign key constraint is incorrectly formed")
我用来创建表格的代码:
CREATE TABLE `oe_modalidad` (
`oferta_establecimiento_id` bigint(20) NOT NULL,
`modalidad_id` bigint(20) NOT NULL,
KEY `fk_oe_modalidades_oferta_establecimiento1_idx` (`oferta_establecimiento_id`),
KEY `fk_oe_modalidad_modalidad1_idx` (`modalidad_id`),
CONSTRAINT `fk_oe_modalidad_modalidad1` FOREIGN KEY (`modalidad_id`) REFERENCES `modalidad` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_oe_modalidades_oferta_establecimiento1` FOREIGN KEY (`oferta_establecimiento_id`) REFERENCES `oferta_establecimiento` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB
我尝试在两个不同版本的 MariaDB 10.0.38 和 5.5.60 中运行它,但我一直收到同样的错误。
【问题讨论】:
-
显示
modalidad和oferta_establecimiento表的定义。 -
搜索你的错误信息,你会看到通常的原因。
-
您可能想阅读此清单以获取正确的外键:stackoverflow.com/a/4673775/20860
-
抱歉信息不足。事实上,这两个键的类型不同。谢谢您的帮助。 @Barmar
-
不要在 many:many 表上使用 FK。如果有一个悬空的引用,这没什么大不了的。
标签: mysql sql foreign-keys mariadb