2

我已经阅读了很多关于这个错误的帖子,但是没有一个解决方案能够解决这个问题(假设我已经正确地尝试了它们)。

这是导致错误的代码:

CREATE TABLE season
(
  id          smallint unsigned NOT NULL auto_increment,
  title       varchar(25) NOT NULL,

  PRIMARY KEY (id)
);

CREATE INDEX seasonId ON season(id);

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

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

所以根据错误,我的外键声明有问题。但是我已经在机器上运行了这段代码,没有任何问题,并且它在我的 Linux 机器上也能完美运行(我目前在 Windows 7 下工作)。

这是输出SHOW ENGINE INNODB STATUS

------------------------
LATEST FOREIGN KEY ERROR
------------------------
120229 17:43:28 Error in foreign key constraint of table fcrcontent/event:
FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
):
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.

我还尝试在新数据库上运行我的脚本,但不行。

这是来自的输出show create table season

| season | CREATE TABLE `season` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(25) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `seasonId` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
4

4 回答 4

4

由于 season.id 是无符号的,event.season_id 也需要无符号:

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
);
于 2012-02-29T17:43:07.880 回答
2

有关“无法创建表 'X' (errno: 150)”的问题,请查看此页面

他指出的最重要的事情是在它发生后立即从命令行登录到您的 mysql 服务器并键入:

显示引擎 INNODB 状态;

这会吐出各种各样的废话,但最重要的是你应该看到一个标题为“最新外键错误”的部分,在那里你会看到实际的问题,说的是这样的:


最新的外键错误

121114 16:22:57 表 dgweb/company 的外键约束错误:引用表中没有包含列作为第一列的索引,或者引用表中的数据类型与表中的数据类型不匹配。约束: , CONSTRAINT "fk_company_wf_reporting_info" FOREIGN KEY ("wf_reporting_info") REFERENCES > "wf_reporting_info" ("wf_reporting_info_id") 表中外键索引为 "fk_company_wf_reporting_info" 见http://dev.mysql.com/doc/refman /5.5/en/innodb-foreign-key-constraints.html 用于正确的外键定义。

然后你就会知道到底出了什么问题:-)。

于 2012-11-14T22:32:27.780 回答
1

由于您尚未显示 的输出show create table season,因此我无法确定您的问题是什么。

但是,MySQL 文档有一个清单:

  • 相应的列 [...] 必须具有相似的内部数据类型。[..]整数类型的大小和符号必须相同

  • InnoDB 需要外键和引用键的索引 [...]。

  • [...] 在引用的表中,必须有一个索引,其中引用的列按相同顺序列为第一列。

(强调我的)。

请确保您的餐桌符合这些标准;如果仍然失败,请阅读文档页面上的其余标准。

于 2012-02-29T17:37:13.990 回答
0

这 :

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

需要与此完全相同的类型:

season_id    smallint NOT NULL,

所以把它改成

smallint(5)
于 2012-03-05T15:49:50.527 回答