MySQL 5.5 errno: 150 “外键约束格式不正确"

MySQL 5.5 errno: 150 quot;Foreign key constraint is incorrectly formedquot;(MySQL 5.5 errno: 150 “外键约束格式不正确)
本文介绍了MySQL 5.5 errno: 150 “外键约束格式不正确"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个错误有很多问题,但我似乎找不到任何与我所拥有的相似的场景.

There are plenty of questions regarding this error but I can't seem to find any similar scenario to what I have.

我的第一张桌子(用户):

My 1st table (users):

我的第二张桌子(大学):

My 2nd table (colleges):

我正在尝试更改第一个表并添加一个引用第二个表的 id 的外键:

I am trying to alter 1st table and add a foreign key that references id of a 2nd table:

ALTER TABLE users
ADD CONSTRAINT FOREIGN KEY (collegelinkId)
REFERENCES databaseName.colleges (id);

失败并返回错误 (errno: 150 "Foreign key constraint is wrongly forms").

这两个表之间唯一不同的参数是auto_increment.但是,我无法将 auto_increment 添加到我的用户表 collegelinkId 列,因为它的 id 已设置为 auto_increment.

The only parameter that is different between these two tables is auto_increment. However, I can not add auto_increment to my users table collegelinkId column since its id is already set to auto_increment.

推荐答案

由于列的类型相同,因此值得按照@Tim Biegleisen 的建议检查引擎类型.

Since the columns are of the same type, it's worth to check the engine type as @Tim Biegeleisen suggested.

更改引擎类型解决了这个问题.

Changing engine type fixed the issue.

ALTER TABLE users
ENGINE=InnoDB;

这篇关于MySQL 5.5 errno: 150 “外键约束格式不正确"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)