MySQL Forums
Forum List  »  InnoDB

Re: ERROR 1005: Can't create table (errno: 150) :: InnoDB
Posted by: Edwin Dando
Date: September 10, 2005 04:31PM

Ok, here is what I have found after hours of ripping my hair out with this!

As other posters have said, both tables must be INNODB. The foreign key field must have an index on it. The foeign key field and the field being referenced must be of the same type (I only use integer) and, after hours of pain, they must be UNSIGNED.

Here is some example code (this is for a recruitment website). Firstly, there are two tables that dont reference each other, s_user and skillset.


DROP TABLE IF EXISTS s_user;
CREATE TABLE s_user (
id_s_user INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
password VARCHAR(20) NOT NULL,
fname VARCHAR(50) NOT NULL,
lname VARCHAR(50) NOT NULL,
email VARCHAR(40) NOT NULL,
PRIMARY KEY(id_s_user)
)TYPE=InnoDB;

DROP TABLE IF EXISTS skillset;
CREATE TABLE skillset (
id_skillset INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
skillset_name VARCHAR(50) NOT NULL UNIQUE,
PRIMARY KEY(id_skillset)
)TYPE=InnoDB;


I know have a table that represents the one to many relationship from s_user to skillset (a user can have many skillsets, for example, a user can know java, php, c++ etc).

DROP TABLE IF EXISTS user_skillset;
CREATE TABLE user_skillset(
id_s_user INTEGER UNSIGNED NOT NULL, INDEX id_s_user_ind (id_s_user),
id_skillset INTEGER UNSIGNED NOT NULL, INDEX id_skillset_ind (id_skillset),
PRIMARY KEY(id_s_user, id_skillset),
FOREIGN KEY(id_s_user) REFERENCES s_user(id_s_user) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(id_skillset) REFERENCES skillset(id_skillset) ON DELETE CASCADE ON UPDATE CASCADE
)TYPE=InnoDB;


Note both FK fields have indexes, and both also use unsigned integers. Also dont forget to create the table being referenced _before_ the one doing the referencing, otherwise there wont be anything to reference!

Hope this helps.

Options: ReplyQuote


Subject
Views
Written By
Posted
647734
March 24, 2005 01:20PM
5316
October 09, 2012 09:48PM
3595
December 19, 2011 10:13PM
Re: ERROR 1005: Can't create table (errno: 150) :: InnoDB
29979
September 10, 2005 04:31PM
9031
May 09, 2006 06:01PM
8214
November 15, 2006 02:05PM
7261
T D
February 01, 2008 06:47AM
9073
August 27, 2008 08:08AM
9579
December 03, 2008 10:15AM
6037
May 01, 2009 07:39AM
3300
August 29, 2011 04:46PM
4876
s l
December 15, 2009 01:48PM
4811
December 22, 2009 02:54AM
3134
February 18, 2011 08:55AM
3721
May 24, 2011 10:11AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.