I am using laravel 5.4. I want to create relationship with two table. one is users table and another is sir table. users table is already created.
here is my sir table migration code
public function up()
{
Schema::create('sir', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('sir');
}
now when I try to migrate it shows me this error
` SQLSTATE[HY000]: General error: 1005 Can't create table
`finalproject`.`#sq
l-9cc_98` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL:
alter table `sir` add constraint `sir_user_id_foreign`
foreign key (`user_id`) references `users` (`id`))`
I have followed some tutorial but those are not working and also tried with laravel documentation, but still it is not working. anyone please help me to find a solution for this. thanks in advance.
Related
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)
I have below migration for database in Laravel,
public function up()
{
Schema::create('tables', function (Blueprint $table) {
$table->bigIncrements('table_id');
$table->string('table_number');
$table->foreignId('section_id')->constrained('sections')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
$table->softDeletes();
});
}
and it throws below error,
SQLSTATE[HY000]: General error: 1005 Can't create table `200120`.`tables` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `tables` add constraint `tables_section_id_foreign` foreign key (`section_id`) references `sections` (`id`) on delete cascade on update cascade)
I am not sure why it references to sections (id), it should reference to sections (section_id)
I did as per this document,
Laravel Doc
Any hints?
you can specify the column name you want to build the relation with:
$table->foreign('section_id')->references('section_id')->on('sections')->onUpdate('cascade')->onDelete('cascade');
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.
I have couples of tables Client, Rooms, and Reservations. When i try to migrate I got this kind of errors:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table
`reservations` add constraint `reservations_client_id_foreign` foreign key (`client_id`) references `clients` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed")
here's my reservation migration code:
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->increments('id');
$table->date('date_in');
$table->date('date_out');
$table->integer('client_id')->unsingned;
$table->foreign('client_id')->references('id')->on('clients');
$table->integer('room_id')->unsingned;
$table->foreign('room_id')->references('id')->on('rooms');
$table->timestamps();
});
}
some said that it's because of my migration order, but I think it's already in the right order and here's the screenshot of it:
so how can i can migrate it without any errors? thanks.
Your spelling is wrong.. unsigned not unsingned and you forget to use ()
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->increments('id');
$table->date('date_in');
$table->date('date_out');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients');
$table->integer('room_id')->unsigned();
$table->foreign('room_id')->references('id')->on('rooms');
$table->timestamps();
});
}
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();
});
}