MySQL ERROR: 1005 Can't create table 'myTable' (errno : 150)

一个人想着一个人 提交于 2019-12-06 07:11:09

Since season.id is unsigned, event.season_id also needs to be unsigned:

CREATE TABLE event
(
  id           smallint unsigned NOT NULL auto_increment,
  title        varchar(255) NOT NULL,
  season_id    smallint unsigned NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
);

For problems with "Can't create table 'X' (errno: 150)", check out this page.

The most important thing he points out, is to login to your mysql server from the command line immediately after it happens and type:

SHOW ENGINE INNODB STATUS;

That will spew out all kinds of crap, but most importantly you should see a section titled "LATEST FOREIGN KEY ERROR", where you'll see the actual problem, saying something like this:


LATEST FOREIGN KEY ERROR

121114 16:22:57 Error in foreign key constraint of table dgweb/company: there is no index in referenced table which would contain the columns as the first columns, or the data types in the referenced table do not match the ones in table. Constraint: , CONSTRAINT "fk_company_wf_reporting_info" FOREIGN KEY ("wf_reporting_info") REFERENCES > "wf_reporting_info" ("wf_reporting_info_id") The index in the foreign key in table is "fk_company_wf_reporting_info" See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html for correct foreign key definition.

Then you'll know what the heck is wrong :-).

Since you haven't shown the output from show create table season, I can't be sure what your problem is.

However, the MySQL docs have a checklist:

  • Corresponding columns [...] must have similar internal data types. [..] The size and sign of integer types must be the same

  • InnoDB requires indexes on foreign keys and referenced keys [...].

  • [...] in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

(emphasis mine).

Please make sure your tables meet these criteria; if it still fails, then read the rest of the criteria on the docs page.

This :

`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,

Needs to be exact same type as this:

season_id    smallint NOT NULL,

so change it to

smallint(5)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!