October 22, 2006

MySQL Error Number 1005 Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)

MySQL Error Number 1005
Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)

If you get this error while trying to create a foreign key, it can be pretty frustrating. The error about not being able to create a .frm file seems like it would be some kind of OS file permission error or something but this is not the case. This error has been reported as a bug on the MySQL developer list for ages, but it is actually just a misleading error message.

In every case this is due to something about the relationship that MySQL doesn’t like. Unfortunately it doesn’t specify what the exact issue is.

First Steps:

If you have admin permission on the server, you may want to start by running the MySQL command “SHOW INNODB STATUS” (or MySQL 5.5 “SHOW ENGINE INNODB STATUS”) immediately after receiving the error. This command displays log info and error details. (Thanks Jonathan for the tip)

If your script runs fine on one server, but gives an error when you try to run it on a different server, then there is a good chance that #6 is the problem.  Different versions of MySQL have different default charset setting and you may have unknowingly assigned different charsets on the different servers.

Known Causes:

Below is a running list of known causes that people have reported for the dreaded errno 150:

  1. The two key fields type and/or size is not an exact match. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same. (More about signed vs unsigned here).
  2. One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field. (thanks to Venkatesh and Erichero and Terminally Incoherent for this tip)
  3. The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this. (Thanks to Niels for this tip)
  4. One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message – it just won’t create the key.) In Query Browser, you can specify the table type.
  5. You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL.  You can fix this by either changing your cascade or setting the field to allow NULL values. (Thanks to Sammy and J Jammin)
  6. Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns. (Thanks to FRR for this tip)
  7. You have a default value (ie default=0) on your foreign key column (Thanks to Omar for the tip)
  8. One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint. (Thanks to Alex for this tip)
  9. You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship (Thanks to Christian & Mateo for the tip)
  10. The name of your foreign key exceeds the max length of 64 chars.  (Thanks to Nyleta for the tip)

The MySQL documentation includes a page explaining requirements for foreign keys. Though they don’t specifically indicate it, these are all potential causes of errno 150. If you still haven’t solved your problem you may want to check there for deeper technical explainations.

If you run into this error and find that it’s caused by something else, please leave a comment and I’ll add it to the list.

237 Comments on “MySQL Error Number 1005 Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150)

Mati
November 29, 2012 at 2:53 am

Thank you!

Reply
Kevin Sharp
December 17, 2012 at 3:08 pm

Thanks for this article, it saved me a lot of frustration. It was problem #4 for me.

Reply
Toxa
December 27, 2012 at 3:18 am

try to see for signed/unsigned in key fields 😉

Reply
Gabe
January 7, 2013 at 11:15 am

I followed the steps but what finally worked for me is:
SET foreign_key_checks = 0;

Reply
Christie
January 12, 2013 at 2:03 pm

Perfect. I hate getting this error message, and this helped me track it down within minutes. Bookmarking for future reference – thanks a ton!

Reply
Stosh
January 15, 2013 at 9:48 am

I have just discovered that it helps to define the table that will be referenced by the foreign key *before* the table with the foreign key.

Reply
Jamie
January 25, 2013 at 11:27 pm

Oh. My. Goodness.

I read through this entire blog and did not find my situation listed…. so I kept working on the problem, and working, and working.

I finally found it.

You see, I’m using MySQL Workbench, which has a nice handy “comments” section that lets you nicely comment on all of your fields. Therein lie the problem for me. I was entering the comments the way I normally would for anything. This included the use of special characters: () ” ‘ . ; @ etc.

I don’t know which one, but one of them messed up my script. You see, I returned to my comments and turned everything into pure text–just letters and numbers. Nothing else and…

IT WORKED.

Here’s hoping I save someone from puling out their hair.

Reply
Damian
February 5, 2013 at 6:48 am

You should also check for typos or changes from development to production in DB/table names.

Reply
Alexandra
March 6, 2013 at 4:49 am

Your post helped me. Thanks !

Reply
Nishant Nawarkhede
March 14, 2013 at 5:20 am

The First one..
The two key fields type and/or size is not an exact match.

Thank you …

Reply
GrayFox
April 12, 2013 at 1:05 pm

My problem was solved by adding an index to the referenciated column of the referenciated table and erasing the data from the other table (the one I wanted to add a foreign key).
Thanks a lot.

Reply
WaveyA
August 9, 2013 at 3:27 am

I’ve been struggling with this problem for years. Wish I had seen this before. Thanks.

Reply
Szparag
August 13, 2013 at 6:42 am

Remember that UNSIGNED attribute is also very important

Reply
Gus
October 28, 2013 at 6:36 am

For some reason I got this error when I accidentally used “table.column” syntax as opposed to “table (column)” for the referenced field.

mysql> ALTER TABLE child ADD CONSTRAINT child_parent_fk FOREIGN KEY (parent_id) REFERENCES parent.id ON UPDATE NO ACTION ON DELETE NO ACTION;
ERROR 1005 (HY000): Can’t create table ‘mydb.#sql-467_26′ (errno: 150)

should be:

mysql> ALTER TABLE child ADD CONSTRAINT child_parent_fk FOREIGN KEY (parent_id) REFERENCES parent (id) ON UPDATE NO ACTION ON DELETE NO ACTION;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

MySQL error messages are a complete mystery to me. They very rarely, if ever, seem to have any connection to the actual problem that occurred. It might as well just say “>Nope.” and put you back at the prompt.

Reply
JoAnn Moak
December 19, 2013 at 10:26 am

I followed through this while looking for a solution to a class assignment and still did not find the answer to my problem. However, I did eventually find it on my own. My table names and my key names were too similar. For example, I had a table named knife_id that contained a key named Knife_Id. I thought they would be different enough having used capital letters in the key. I was wrong. I kept getting this error until I removed _id from the table name. Then it finally accepted it.

Hopefully, this might help someone else who has this issue.

Reply
KevinW
April 11, 2014 at 5:23 pm

My base table was “user” and my foreign key was trying to reference a table named “users”. Took me a while to realize that I had mistyped the table name.

Reply
Prakriti
May 8, 2014 at 11:31 pm

Thanks. I didn’t realise a foreign key existed with the same name. The error message is quite unhelpful.

Reply
Amol
June 9, 2014 at 11:28 pm

Thanks a Lot! Solved our issue with Cause/solution No.-3. Really great post.

P.S. – Just can not believe this post was from 2006!

Reply
Giovanni
August 19, 2014 at 12:01 pm

2014 and still useful.

My problem was (6): different table charset.

Reply
Roma
August 28, 2014 at 5:19 am

got errno:150
Solution was to rename referenced table `aux` to anything else.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>