Table "types" should be created before "unites".
May 5, 2022
10
Level 1
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
Please or to participate in this conversation.