Problem when I run the ‘artisan:migrate’ in a ‘Many to One’ relationship

I have the ‘people’ table and each ‘person’ must have some ‘furniture’.

I run ‘migrate’ and this error message appears:

QLSTATE[HY000]: General error: 1005 Can’t create table
my_database.furniture (errno: 150 “Foreign key constraint is
incorrectly formed”) (SQL: alter table furniture add constraint
furniture_person_id_foreign foreign key (person_id) references
people (id))

LARAVEL VERSION: 5.8

Here are my files:

2021_04_15_132404_create_persons_table.php

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreatePeopleTable extends Migration
{
   
    public function up()
    {
        Schema::create('people', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('cpf');
            $table->string('phone');
            $table->softDeletes();      
            $table->timestamps();          
        });
    }
  
   
    public function down()
    {
        Schema::dropIfExists('people');
    }
}

2021_04_16_233323_create_furniture_table.php

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateFurnitureTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('furniture', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('type');
            $table->string('name');
            $table->integer('person_id')->unsignedBigInteger()->nullable();
            $table->foreign('person_id')->references('id')->on('people');
            $table->softDeletes();   
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('furniture');
    }
}

Person.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;

class Person extends Model
{
    use SoftDeletes;
  
    
}

Furniture.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;

class Furniture extends Model
{
    use SoftDeletes;
}

Does anyone know how to solve?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Remove $table->integer('person_id')->nullable(); from CreateFurnitureTable migration, leave the unsignedBigInteger since that’s the integer column type. Just using that method us also recommended by Laravel as per the documentation on foreign keys for Laravel 5.8

$table->unsignedBigInteger('person_id');

$table->foreign('person_id')->references('id')->on('people');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x