我已经阅读了很多关于这个错误的帖子,但是没有一个解决方案能够解决这个问题(假设我已经正确地尝试了它们)。
这是导致错误的代码:
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 |