当我在"多对一"关系中运行"工匠:迁移"时出现问题



我有"人"的桌子,每个"人"都必须有一些"家具"。

我运行"迁移",出现以下错误消息:

QLSTATE[HY000]:一般错误:1005无法创建表CCD_ 1。furniture(错误号:150"外键约束为不正确地形成"((SQL:更改表furniture添加约束furniture_person_id_foreign外键(person_id(引用people(id((

LARAVEL版本:5.8

这是我的文件:

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;
}

有人知道怎么解决吗?

CreateFurnitureTable迁移中删除$table->integer('person_id')->nullable();,保留my_database0,因为它是整数列类型。仅使用该方法,我们也被Laravel根据Laravel 5.8 的外键文档推荐

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

最新更新