【问题标题】:Problem when I run the 'artisan:migrate' in a 'Many to One' relationship当我以“多对一”关系运行“工匠:迁移”时出现问题
【发布时间】:2021-04-17 06:14:16
【问题描述】:

我有“人”表,每个“人”都必须有一些“家具”。

我运行“迁移”并出现此错误消息:

QLSTATE[HY000]:一般错误:1005 无法创建表 my_database.furniture (errno: 150 "外键约束为 格式不正确”) (SQL: alter table furniture add constraint furniture_person_id_foreign 外键 (person_id) 引用 people (id))

LARAVEL 版本:5.8

这是我的文件:

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

有人知道怎么解决吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    CreateFurnitureTable迁移中删除$table-&gt;integer('person_id')-&gt;nullable();,留下unsignedBigInteger,因为那是整数列类型。只需使用 Laravel 也推荐的这种方法,根据 documentation on foreign keys for Laravel 5.8

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

    【讨论】:

    • 返回错误信息:方法 Illuminate\Database\Schema\Blueprint::foreignId 不存在。此方法可能在 5.8 版本中不存在。这是我正在使用的版本。不是吗?
    • 更新了 Laravel v5.8 的答案。
    猜你喜欢
    • 2015-04-06
    • 2021-06-15
    • 2016-12-29
    • 2020-10-27
    • 2015-08-12
    • 2011-08-19
    • 2016-07-08
    • 1970-01-01
    • 2018-09-16
    相关资源
    最近更新 更多