Laravel migration error in foresign key 150 - laravel

I started working on small project using Laravel latest version, i try to make a foreign key between two table ( Buildings and Appartmens ) but i get an error message contain :
("SQLSTATE[HY000]: General error: 1005 Can't create table `project`.`apartments` (errno: 150 "Foreign key constraint is incorrectly formed")")
this is my buildings table schema :
Schema::create('buildings', function (Blueprint $table) {
$table->id();
$table->string('address');
$table->timestamps();
});
this is my apartments table schema :
Schema::create('apartments', function (Blueprint $table) {
$table->id();
$table->string('number');
$table->integer('monthly_price');
$table->integer('rooms');
$table->integer('bath_room');
$table->string('description');
// Foreign Key
$table->foreignId('building_id')->constrained('buildings');
// Record Times
$table->timestamps();
});

It looks like your apartments migration runs before your buildings migration, so no foreign key can be created because the buildings table has not yet been created.
To fix this you can change the date that prefixes the migration, for example:
2020_08_03_140214_create_apartments_table
2020_08_03_140387_create_buildings_table
to:
2020_08_03_140387_create_buildings_table
2020_08_03_140388_create_apartments_table
Don't forget to run a composer dump-autoload afterwards in order to update the class maps as well.

Related

how to solve this erorr SQLSTATE[HY000]: General error: 1005 Can't create table

I have this error when run migration
SQLSTATE[HY000]: General error: 1005 Can't create table tms-app.#sql-1e64_2b (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table projects add constraint projects_cout_id_foreign foreign key (cout_id) references couts (id) on update cascade)
this is projects table:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->string('libelle_long');
$table->string('direction');
$table->integer('cout_id')->unsigned();
$table->foreign('cout_id')
->references('id')->on('couts')
->onUpdate('cascade');
$table->foreign('status')
->referenecs('id')->on('statuses')
->onUpdate('cascade')
->onDelete('cascade');
$table->timestamps();
});
Sometimes this error happens, when we define foreign key and you should use bigInteger('') or use unsignedBigInteger('').
Use the code below:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->string('libelle_long');
$table->string('direction');
$table->bigInteger('cout_id')->unsigned();
$table->bigInteger('status')->unsigned();
$table->foreign('cout_id')->references('id')->on('couts')->onDelete('cascade');
$table->foreign('status')->references('id')->on('statuses')->onDelete('cascade');
$table->timestamps();
});
Note: In tables couts & statuses change the id field $table->increments('id'); to $table->bigIncrements('id');
This error should be casuses by the following miss-configuration below I mentioned, be sure all of them to be configured correctly
1: Laravel migration foriegn key should be assgined as bigInteger or unsignedBigInteger
example:
$table->unsignedBigInteger('user_id');
or
$table->bigInteger('user_id')->unsigned();
and for assigning foriegn key attribute you should do
$table->foriegn('user_id')->reference('id')->on('users'); // if you need further attribtute you can chain to this line
2: be sure that the order of migration file is formed correclty because Laravel will migrate table based on timestamp for example if need a foreign key from user table first you need to create user table then the child table the timestamp of users table should be older than child (even 1 second)

Why it appears the error "General error: 1005 Can't create table"?

I have these two migration files one to create a faq_channels table and another to create a faq_questions table. However, I'm not understanding why it is showing an error:
SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`faq_questions` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `faq_questions` add const
raint `faq_questions_channel_id_foreign` foreign key (`channel_id`) references `faq_channels` (`id`) on delete cascade)
Do you know why?
// create_faq_channels_table
public function up()
{
Schema::create('faq_channels', function (Blueprint $table) {
$table->id();
$table->string('channel');
$table->text('description');
$table->timestamps();
});
}
// create_faq_questions_table
Schema::create('faq_questions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('channel_id');
$table->string('question');
$table->text('response');
$table->timestamps();
$table->foreign('channel_id')->references('id')->on('faq_channels')->onDelete('cascade');
});
I have these two migration files one to create a faq_channels table
and another to create a faq_questions table.
Make sure that migration file for 'faq_channels' table runs before the migration file for 'faq_questions' table. Generally this type of error rises when the parent table is not created before declaring foreign key constraint.
Also edit your migration files according to Talha F.'s answer.

Foreign Key problem when migrating migrations in laravel 5.6

Am working on a Laravel 5.6 application whereby I have 2 tables mainly sponsors table and children table. Am creating a one to many relationship between the tables before migrating them. A child may have many sponsors.
The problem is I get this error when migrating them using php artisan migrate command:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error:
1005 Can't create table larangular.#sql-520c_21f (errno: 150
"Foreign key constraint is incorrectly formed") (SQL: alter table
sponsors add constraint sponsors_child_id_foreign foreign key
(child_id) references children (id) on delete cascade)
Sponsors migration
public function up()
{
Schema::create('sponsors', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('child_id')->unsignedInteger();
$table->foreign('child_id')->references('id')->on('children')->onDelete('cascade');
$table->foreign('child_id')->references('id')->on('children');
$table->string('email')->unique();
$table->string('phone');
$table->string('nationality');
$table->timestamps();
});
}
Children migration
public function up()
{
Schema::create('children', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age');
$table->string('gender');
$table->timestamps();
});
}
Make sure that your children migration runs before your sponsors migration, and then the column should be this:
$table->unsignedInteger('child_id');
// or
$table->integer('child_id')->unsigned();
unsignedInteger is a function that creates a column, you should not call it on an existing column.
// This creates an unsigned integer column.
$table->unsignedInteger('child_id');

Delete each corresponding row in One-to-One relationship on delete of either

I am trying to learn one-to-one relations. I am using Laravel 5.5.13.
My simple app is this:
I create a App\Message. I optionally can associate a App\Task with it.
My goal:
Once a App\Task is associated, if the task is deleted, it should cascade delete the App\Message row.
And the reverse, if the App\Message is deleted, it should cascade and delete the App\Task row.
However I am having an error on migrate because the tasks_table is created AFTER the messages_table.
Here is my migration:
2017_10_15_021803_create_messages_table.php:
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('task_id')->unsigned()->nullable();
$table->text('body');
$table->timestamps();
$table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade'); //// IF I COMMENT THIS OUT THE MIGRATION WORKS, BUT I NEED THIS IN
});
}
And for App\Task 2017_10_15_023343_create_tasks_table.php:
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('message_id')->unsigned();
$table->timestamps();
$table->foreign('message_id')->references('id')->on('messages')->onDelete('cascade');
});
}
If I comment out the $table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade'); then the migration works, BUT i need the deletion of the message when the task is deleted, and without this line that won't happen.
The error I get after running php artisan migrate is:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table petfolk.#sql-42e8_16f (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table messages add constraint messages_task_id_foreign foreign key (task_id) references tasks (id) on delete set null)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table petfolk.#sql-42e8_16f (errno: 150 "Foreign key constraint is incorrectly formed")
Just create third migration and move foreign key adding logic in the migration:
Schema::table('messages', function (Blueprint $table) {
$table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade');
}
It will work when both tables will be created.
A one-to-one relationship links one row in a database table to one (and only one) row in another table.
onDelete('cascade') is used when there is an intermediate, also known as a pivot, table between two tables in a many-to-many relationship. You do not need this for a one-to-one relationship because the link to the messages table only exists in the row of the tasks table (and that is being deleted).
Try this for the tasks table migration:
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('message_id')->unsigned()->nullable();
$table->foreign('message_id')->references('id')->on('messages');
$table->timestamps();
});
}

General error: 1005 Can't create table ,Foreign key constraint is incorrectly formed in laravel

why laravel schema reply
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table test.#sql-13cc_d0 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table cities add constrai
nt cities_provinces_id_foreign foreign key (provinces_id) references provinces (id) on delete cascade)
[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create
table test.#sql-13cc_d0 (errno: 150 "Foreign key constraint is
incorrectly formed")
first table
Schema::create('provinces', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();
});
second table
Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('province_id')->unsigned()->index();
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();
$table->foreign('province_id')
->references('id')->on('provinces')
->onDelete('cascade');
});
You should make sure, your provinces table migration is running before cities table migration.
You should change provinces or cities migration file name to make sure timestamp at the beginning of provinces table migration file will be before cities table.
The time go ahead but i will comment something else.
Andile is right! You must have the same type. In this case, the foreign key should have the type bigInteger because you have bigIncrements on the reference table.
I think thats the solution for a little and boring debug. (sorry for my english)
Try to create table first and then add constraint:
Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('province_id')->unsigned()->index();
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();
});
Schema::table('cities', function (Blueprint $table) {
$table->foreign('province_id')->references('id')->on('provinces')->onDelete('cascade');
});
The foreign key and referencing table must be same type, the auto generated id are of bigInteger, so instead of defining your column as Integer use bigInteger insteated. That Solved the problem for me

Resources