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 tablefurnitureadd constraintfurniture_person_id_foreignforeign key (person_id) referencespeople(id))
LARAVEL VERSION: 5.8
Here are my files:
2021_04_15_132404_create_persons_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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 Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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 Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Person extends Model
{
use SoftDeletes;
}
Furniture.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Furniture extends Model
{
use SoftDeletes;
}
Does anyone know how to solve?