Solved: MySQL ERROR 1005: Can’t create table (errno: 150) (Foreign Key)

This is another stupid error. It has to do with trying to successfully set foreign keys in MySQL.

ERROR 1005: Can’t create table (errno: 150)

Great, that’s fantastic. Here’s an example of where this error will occur.

CREATE TABLE main(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE other(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
main_id INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(main_id) REFERENCES main(id)
);

So I’m trying to make the table “other” reference the table “main” through the foreign key “main_id” and, if you try it, it’ll throw an Error 150.

Want the solution?

The foreign key “main_id” has to have the exact same type as the primary key that it references. In the example, “main_id” in the table “other” has the type INT NOT NULL while “id” in the table “main” has the type “INT UNSIGNED NOT NULL” and also AUTO_INCREMENT, but that isn’t something we have to worry about. To make things incredibly clear, here’s the working example.

CREATE TABLE main(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE other(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
main_id INT UNSIGNED NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(main_id) REFERENCES main(id)
);

To solve ‘MySQL ERROR 1005: Can’t create table (errno: 150)‘ you likely just have to ensure that your foreign key has the exact same type as the primary key. Hope it helps.

62 thoughts on “Solved: MySQL ERROR 1005: Can’t create table (errno: 150) (Foreign Key)”

    1. This might be another answer, though I haven’t tested it. If so, you can make sure your tables are INNODB by specifying in the CREATE TABLE statement:
      ENGINE=InnoDB;
      Or you can alter existing tables with this:
      ALTER TABLE my_table ENGINE = InnoDB;

  1. Espero esto le sirva a alguien, investigando sobre este error se produce por dibersas causas las principales que no cohinciden la llave primaria con la foranea, es decir tienen tipos diferentes, es decir int varchar etc, o tienen atributos diferentes como por ejemplo el unsigned o binary etc, si es el caso anterior se soluciona poniendo tanto llave primaria como foranea del mismo tipo las dos y con los mismos atributos, tambien es recomendado que sean de la misma longitud ya que si bien es sierto que la longitud no es tomada encuenta al momento de relacionar puede causar problemas al momento de pasar a la incercion de datos y demas dml.
    Otro motivo por el cual ocurre este error es por que esta repetido el nombre de campo
    es decir puede que en la bd ya haya un campo con el nombre id por lo que la solucion seria simplemente cambiar el nombre a id1 por ejemplo.

    otro motivo por el cual ocurre este error es el tipo demotor usado en las tablas es decir puede ser que una sea innodb y la otra myisam la solucion es que las dos deben ser del mismo tipo de motor.
    Por ultimo el motivo puede ser tambien el constrainter puede que este configurado como on delete set null y el campo noadmite valores nulos es decir es not null
    Como ya lo habia dicho espero le sirva a alguien esta info y si es asi pueden pegar el comentario en algun otro foro para ayudar a mas gente, saludos

  2. Thanks for writing such a good post to motivate the world to do some better things. Everyone have a cheap and but comfort zone where they feel relaxed. But there are rare people in the world accept the challenges and give something different what people looking for. I am sure people will enjoy your tips.

  3. Pingback: Google

  4. Pingback: URL

  5. Pingback: Google

  6. Pingback: Google

  7. Pingback: Google

  8. Pingback: Google

  9. Pingback: tottally free dating sites

  10. Pingback: Google

  11. Pingback: Top 7 Error 1005 Hy000 Can T Create Table The 27 Latest Answer

  12. Pingback: Mysql Errno 150? Top 7 Best Answers - Budget-template.com

  13. Pingback: Mysql Error 1005? Top 7 Best Answers - Budget-template.com

  14. Pingback: online pharmacies

  15. Pingback: madridbet

  16. Pingback: madridbet

  17. Pingback: meritking

  18. Pingback: sex

  19. Pingback: fuvk google

  20. Pingback: fuck google

  21. Pingback: madridbet

  22. Pingback: Ошибка ссылку foreign key невозможно добавить 1005

  23. Pingback: Error 1005 mysql

  24. Pingback: Error code 1005 sql

  25. Pingback: madridbet

  26. Pingback: meritking giriş

  27. Pingback: Error code 150 in mysql

  28. Pingback: Sql create table error 150

  29. Pingback: Sql general error 1005

  30. Pingback: Sql error 1005 foreign key constraint

  31. Pingback: Ошибка 1005 mysql — Решение и исправление самых разных ошибок

  32. Pingback: Ошибка ссылку foreign key невозможно добавить 1005 — Большая энциклопедия ошибок и их решений

  33. Pingback: child porn

  34. Pingback: child porn

  35. Pingback: grandpashabet

  36. Pingback: grandpashabet

  37. Pingback: child porn

  38. Pingback: cratosroyalbet

  39. Pingback: child porn

  40. Pingback: child porn

  41. Pingback: child porn

  42. Pingback: child porn

  43. Your posts in this blog really shine! Glad to gain some new insights, which I happen to also cover on my page. Feel free to visit my webpage Seoranko about Cosplay and any tip from you will be much apreciated.

  44. Pingback: spinco

  45. You’ve done an impressive work on your website in covering the topic. I am working on content about Thai-Massage and thought you might like to check out YH6 and let me what you think.

  46. Pingback: child porn

  47. Your writing style is cool and I have learned several just right stuff here. I can see how much effort you’ve poured in to come up with such informative posts. If you need more input about Airport Transfer, feel free to check out my website at 46N

  48. Pingback: child porn

  49. Pingback: Buy followers instagram

  50. Pingback: web sitesi kurma

  51. Pingback: cristiano ronaldo skills

  52. Pingback: child porn

Leave a Comment

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