0

Here is my SQL script

CREATE TABLE tracks(
track_id int NOT NULL AUTO_INCREMENT,
account_id int,
track_name varchar(255),
track_path varchar(255),
track_art_path varchar(255),
track_desc text,
primary key(track_id),
FOREIGN KEY (account_id) REFERENCES accounts_profile(accnt_id)
)

I don't see any syntax errors. Everything looks fine. My Database Engine is innoDB. but how come I keep on receiving this error?

#1005 - Can't create table 'beatbeast.tracks' (errno: 150)

It's not showing what line where the error is.

Ben
  • 51,770
  • 36
  • 127
  • 149
user962206
  • 15,637
  • 61
  • 177
  • 270

2 Answers2

2

From the documentation:

If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.

Check that the datatype of accounts_profile.accnt_id matches tracks.account_id exactly. Currently one is an int, so the other must also be an int.

Furhter, the documentation suggests to call:

SHOW ENGINE INNODB STATUS

after you get the error message for a more detailed explanation.

Andomar
  • 232,371
  • 49
  • 380
  • 404
2

Errno 150 is generally the result of a mismatch between the exact data types of the main table's referenced column, and the referencing column. In your case, tracks.account_id is a signed INT, but the column it references accounts_profile.accnt_id is INT UNSIGNED. So you must create the tracks table using INT UNSIGNED for account_id as well:

CREATE TABLE tracks(
  track_id int NOT NULL AUTO_INCREMENT,
  account_id int UNSIGNED,
  track_name varchar(255),
  track_path varchar(255),
  track_art_path varchar(255),
  track_desc text,
  primary key(track_id),
  FOREIGN KEY (account_id) REFERENCES accounts_profile(accnt_id)
)
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390