Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel migration: "Foreign key constraint is incorrectly formed" (errno 150)

When migrating my DB, this error appears. Below is my code followed by the error that I am getting when trying to run the migration.

Code

public function up() {     Schema::create('meals', function (Blueprint $table) {         $table->increments('id');         $table->integer('user_id')->unsigned();         $table->integer('category_id')->unsigned();         $table->string('title');         $table->string('body');         $table->string('meal_av');         $table->timestamps();         $table->foreign('user_id')             ->references('id')             ->on('users')             ->onDelete('cascade');         $table->foreign('category_id')             ->references('id')             ->on('categories')             ->onDelete('cascade');     }); }   

Error message

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table meal.#sql-11d2_1 4 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter
table meals add constraint meals_category_id_foreign foreign key (category_id) references categories (id) on delete cascade)

like image 752
Muhammad Tareq Avatar asked Sep 19 '15 15:09

Muhammad Tareq


People also ask

How to fix errno 150 Foreign key constraint is incorrectly formed?

Answer. The problem is that you have the company table reference the branch and contact table before they are created. You have to create the contact table first but without the foreign id reference to bid, then create branch table and then company table.

What is the foreign key constraint?

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.

What is a foreign key column?

A foreign key is a column (or combination of columns) in a table whose values must match values of a column in some other table. FOREIGN KEY constraints enforce referential integrity, which essentially says that if column value A refers to column value B, then column value B must exist.


2 Answers

When creating a new table in Laravel. A migration will be generated like:

$table->bigIncrements('id'); 

Instead of (in older Laravel versions):

$table->increments('id'); 

When using bigIncrements the foreign key expects a bigInteger instead of an integer. So your code will look like this:

public function up()     {         Schema::create('meals', function (Blueprint $table) {             $table->increments('id');             $table->unsignedBigInteger('user_id'); //changed this line             $table->unsignedBigInteger('category_id'); //changed this line             $table->string('title');             $table->string('body');             $table->string('meal_av');             $table->timestamps();              $table->foreign('user_id')                 ->references('id')                 ->on('users')                 ->onDelete('cascade');              $table->foreign('category_id')                 ->references('id')                 ->on('categories')                 ->onDelete('cascade');         });     }   

You could also use increments instead of bigIncrements like Kiko Sejio said.

The difference between Integer and BigInteger is the size:

  • int => 32-bit
  • bigint => 64-bit
like image 183
Swooth Avatar answered Oct 07 '22 19:10

Swooth


@JuanBonnett’s question has inspired me to find the answer. I used Laravel to automate the process without considering the creation time of the file itself. According to the workflow, “meals” will be created before the other table (categories) because I created its schema file (meals) before categories. That was my fault.

like image 45
Muhammad Tareq Avatar answered Oct 07 '22 18:10

Muhammad Tareq