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 tablemeal
.#sql-11d2_1 4
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter
tablemeals
add constraint meals_category_id_foreign foreign key (category_id
) referencescategories
(id
) on delete cascade)
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.
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.
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.
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:
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With