简体   繁体   English

MySQL errno: 150 无法创建表

[英]MySQL errno: 150 can't create table

I know there are a lot of questions on SO regarding this error, but even applying what I've gleaned from those, I still get "can't create table (errno: 150)"我知道关于这个错误有很多关于 SO 的问题,但即使应用我从这些中收集的信息,我仍然得到“无法创建表(错误号:150)”

CREATE TABLE sch_results
(
id INT NOT NULL AUTO_INCREMENT,
s_id INT UNSIGNED NOT NULL,
r_eid VARCHAR(10) NOT NULL,
cohorts INT,
allstudents INT,
comments TEXT,
PRIMARY KEY (id),
FOREIGN KEY (s_id) REFERENCES scholarships (id),
FOREIGN KEY (r_eid) REFERENCES sch_recommenders (eid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I've checked that the foreign keys are both stored exactly under that name in their respective tables.我已经检查过外键是否完全以该名称存储在各自的表中。
I've checked that their respective data types match this query.我已经检查过它们各自的数据类型是否与此查询匹配。 I've checked that the two existing tables are using InnoDB and utf8.我检查了两个现有表是否使用 InnoDB 和 utf8。
I added the UNSIGNED NOT NULL to the foreign keys.我在外键中添加了 UNSIGNED NOT NULL。
I indexed the two foreign keys in their tables.我在他们的表中索引了两个外键。 Initially, I was using s_id and r_id, but heard this might cause some problems.最初,我使用 s_id 和 r_id,但听说这可能会导致一些问题。 r_eid is also unique, so I switched to that. r_eid 也是唯一的,所以我切换到那个。 It is not the primary key.它不是主键。

What am I missing?我错过了什么?

For essentially all the causes of this error, here is an exhaustive resource for what causes errno 150 (and errno 121/other foreign key errors) in MySQL.对于此错误的基本上所有原因,这里是导致 MySQL 中 errno 150(和 errno 121/其他外键错误)的原因的详尽资源。

MySQL Foreign Key Errors and Errno 150 MySQL 外键错误和 Errno 150

The problem is that the source and target fields need to be the same in every aspect.问题是源字段和目标字段在各个方面都需要相同。 For example, if the source field has "unsigned" off, and the target has "unsigned" on, you will get this error.例如,如果源字段“无符号”关闭,而目标字段“无符号”打开,您将收到此错误。

Turns out the collation was set to utf8-unicode-ci instead of utf8-general-ci.结果发现排序规则设置为 utf8-unicode-ci 而不是 utf8-general-ci。 Fixed it and it works now.修复了它,现在可以使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM