【问题标题】:MySQL InnoDB table adding foreign key errno:150MySQL InnoDB 表添加外键 errno:150
【发布时间】:2012-10-17 02:54:01
【问题描述】:

嗯,我有两张桌子

publica_evento_grupo

Field | Type | Null | Key | Default | Extra
id_evento_grupo int(10) unsigned    NO  PRI     auto_increment
id_evento       int(8)  unsigned    NO  MUL     
id_grupo        int(8)  unsigned    NO  MUL     
identificacao   varchar(55)         NO  

publica_identificacao_publicacao

Field | Type | Null | Key | Default | Extra
id_evento       int(8) unsigned NO  PRI     
identificacao   varchar(55)     NO  PRI     

publica_evento_grupo.id_evento in 是名为 publica_evento 的第三个表的外键,但与表 publica_identificacao_publicacao.identificacao 的列一起也是外键。问题是,我必须通过键 id_evento 创建将 publica_evento_grupopublica_identificacao_publicacao 相关联的外键,但是当我尝试使用 @ 列创建另一个 FK 时987654329@,它给出了下面的errno

 [Err] 1005 - Can't create table '#sql-1049_1980992' (errno: 150).

如您所见,表 publica_identificacao_publicacao 有两个 PK,这就是它必须是 2 个 FK 才能关联它们的原因,我还没有创建索引,因为就我而言知道,我只需要在添加 FK 约束后创建索引,并且我能够在 evento_grupoidentificacao_publicacao 之间为 id_evento 列创建 FK 约束,我不知道为什么只有 identificacao 列出现此错误

编辑 1:@RolandBouman 我无权使用该命令 SHOW ENGINE INNODB STATUS

编辑 2:@Geoff_Montee 实际上您传递的命令有效,不明白为什么我使用该结构,看看这个问题MySQL UNIQUE Constraint multiple columns condition

【问题讨论】:

  • 遇到此错误后立即执行SHOW ENGINE INNODB STATUS,并在输出中查找标题LAST FOREIGN KEy ERROR。可能这只是数据类型不匹配。

标签: mysql sql entity-relationship foreign-key-relationship indexing


【解决方案1】:

如果没有实际的 DDL,就很难说。我建议这样做:

SHOW ENGINE INNODB STATUS;

在您遇到此错误后立即。在输出中,查找如下所示的部分:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
121026 22:40:18 Error in foreign key constraint of table test/#sql-154c_94:
foreign key(rid) references bla(id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

您会在此处找到有关外键定义有什么问题的详细信息。

【讨论】:

  • 好命令大家都知道。我敢打赌,这是因为他希望仅对主键的一部分进行外键约束。从逻辑上讲,这对我来说似乎没有意义。
  • 嘿,谢谢。其实我没有仔细阅读这个问题。奇怪的是,Innodb 实际上允许您在父表上的任何索引的前缀上创建外键。即使那不是唯一索引或主键,即使前缀不是唯一的。古怪,并且没有任何实际的用例,但实际上是允许的。 (天知道为什么)
【解决方案2】:

该错误通常发生是因为引用表和被引用表之间存在类型不匹配。在这种情况下,类型似乎匹配。

您是否考虑过对主复合键中的两个字段都设置约束?您可能必须首先删除 id_evento 上的现有外键约束。

ALTER TABLE publica_evento_grupo ADD FOREIGN KEY 
    (id_evento, identificacao) REFERENCES 
    publica_identificacao_publicacao (id_evento, identificacao);

似乎这种情况下的错误可能是因为您尝试将外键约束添加到复合主键的部分

identificacao 是否存在于任何其他表中?为什么要只对复合主键的一部分添加外键约束?

这样想吧……

假设您在publica_evento_grupo 表中对identificacao 字段本身有一个外键约束。假设在publica_identificacao_publicacao 表中您有(1, "some string")(2, "some string")。因此,如果您删除 (2, "some string"),但将 (1, "some string") 留在原处。 publica_evento_grupo 表中引用 (1, "some string") 的行会报错,即使它们不应该报错!

所以在我看来,您应该为所有主键添加外键约束,或者一个都不添加。

【讨论】:

  • 你说的很有道理,谢谢,我会把你的答案标记为正确的! :D
猜你喜欢
  • 1970-01-01
  • 2015-06-20
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 2011-05-03
  • 1970-01-01
  • 2018-09-04
相关资源
最近更新 更多