Laravel-迁移,表结构修改-正确方法

问题描述 投票:0回答:1

我现在的桌子是

Schema::create('students', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('first_name', 255);
    $table->string('last_name', 255);
    $table->enum('gender', ['m', 'f']);
    $table->date('date_of_birth');
    $table->integer('roll_number');
    $table->char('section', 1);
    $table->integer('class');
    $table->unsignedBigInteger('school_id');            
    $table->string('photo')->nullable;
    $table->timestamps();

    $table->foreign('school_id')
        ->references('id')->on('schools')
        ->onUpdate('cascade')->onDelete('cascade');

    $table->unique(['roll_number', 'section', 'class', 'school_id']);
});

标准

Schema::create('standards', function (Blueprint $table) {
       $table->bigIncrements('id');
       $table->string('name');
       $table->unsignedBigInteger('school_id');
       $table->timestamps();

       $table->foreign('school_id')
       ->references('id')->on('schools')
       ->onUpdate('cascade')->onDelete('cascade');
    });

部分

Schema::create('sections', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->char('name', 1);
    $table->unsignedBigInteger('standard_id');
    $table->timestamps();

    $table->foreign('standard_id')
    ->references('id')->on('standards')
    ->onUpdate('cascade')->onDelete('cascade');
});

现在我有了标准和节表,这些表中的外键将替换现有结构中的类和节列,并保持roll_numbersection_idstandard_idschool_id的组合为唯一。

我尝试过

 public function up()
    {
        Schema::table('students', function (Blueprint $table) {
            $table->dropUnique(['roll_number', 'section', 'class', 'school_id']);

            $table->dropColumn('section');
            $table->dropColumn('class');
            $table->unsignedBigInteger('standard_id')->after('roll_number');
            $table->unsignedBigInteger('section_id')->after('standard_id');

            $table->foreign('standard_id')->references('id')->on('standards')
            ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('section_id')->references('id')->on('sections')
            ->onUpdate('cascade')->onDelete('cascade');

            $table->unique(['roll_number', 'standard_id', 'section_id', 'school_id']); // unique combination
        });
    }

但是它似乎不起作用。

错误

Illuminate \ Database \ QueryException:SQLSTATE [23000]:完整性约束违反:1452无法添加或更新子行:外部键约束失败(myapp_extra#sql-2f78_29d,CONSTRAINTstudents_standard_id_foreign外键(standard_id)参考NCES standardsid)ON DELETE CASCADE ON UPDATE CASCADE)(SQL:更改表students添加约束students_standard_id_foreign外键(standard_id)在删除时引用standardsid)在更新级联上级联)

  at \myapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664

:在进行此迁移之前已创建标准和截面表,因此两列均可用。

php mysql laravel laravel-5.8 laravel-migrations
1个回答
-1
投票

确保模型中的standard_idsection_idfillable

class Student extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['standard_id', 'section_id`];
}
© www.soinside.com 2019 - 2024. All rights reserved.