I've read a number of posts about this error, but none of the solutions have managed to solve the problem (assuming I've tried them correctly).
This is the code that causes the error:
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
);
So according to the error there is a problem with my foreign key declaration. However I had already run this code on the machine with no problems, and it ran perfectly on my Linux Machine as well (I'm currently working under Windows 7).
Here is the output of 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.
I also tried running my script on a fresh database, but no go.
Here is the output from 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 |
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)
来源:https://stackoverflow.com/questions/9503334/mysql-error-1005-cant-create-table-mytable-errno-150