Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Syrine123's avatar

Why I get error on migrating table with foreign key ?

Hello evryone, Actually I want to migrate a table with 3 foreign keys .however it works with 2 but the for the other I usually get this error: (errno: 150 "Foreign key constraint is incorrectly formed") this is the first table :

  */
     public function up()
    {
        Schema::create('unites', function (Blueprint $table) {
            $table->increments('unit_id');
            $table->unsignedInteger('building_id')->nullable();
            $table->foreign('building_id')->references('building_id')->on('buildings');
            $table->unsignedInteger('floor_id')->nullable();
            $table->foreign('floor_id')->references('floor_id')->on('floors');  
            $table->unsignedInteger('type_id')->nullable();
            $table->foreign('type_id')->references('type_id')->on('types'); 
            $table->string('unit_name',200);
            $table->decimal('unit_rent_per_month', 20,3); 
            $table->tinyinteger('unit_status');
            $table->integer('unit_roomnumber');
            $table->json('unit_pictures');
            $table->date('unit_added_date')->format("yyyy-MM-dd");
             $table->timestamps();
        });
    }

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

    
}

this is the other table:

 public function up()
    {
        Schema::create('types', function (Blueprint $table) {
            $table->increments('type_id');
            $table->string('unit_type', 20);
            $table->string('user_type', 20);
            $table->string('periodicity', 20);
            $table->string('currency', 10);
            $table->timestamps();
        });
    }

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

PS: the date of migration of types table is before the unites table and thanks in advance for your help

0 likes
10 replies
petrit's avatar

Table "types" should be created before "unites".

CrastyCrap's avatar

to construct a foreign key the type of the foreign key on your table must be the same as the reference one so you must to make sure that id at building table is unsigned integer and not unsignedInteger overall i don't recommend you to make foreign by that way u can simply create it by just use foreignId

 $table->foreignId('building_id');
tykus's avatar

@CrastyCrap foreignId will make an unsigned big integer but the PK on the other table is just unsigned integer, so the @syrine123 has the correct type.

@petrit the OP has said types table exists before unites table:

the date of migration of types table is before the unites table

@syrine123 do you know for sure that the problem is the type_id FK?

1 like
Syrine123's avatar

@CrastyCrap it worked for building_id and floor_id but I didn't know what's the wrong with type_id

Syrine123's avatar

@tykus yes the other foreign keys are stored in the table unites but type_id not..the table table is already before unites

Syrine123's avatar

@tykus oupps the problem was because I delete types table and I forgot to refresh migration for it

Please or to participate in this conversation.