Laravel 5.6设置迁移可为空的外部ID

时间:2019-01-29 14:32:47

标签: laravel migration laravel-5.6

  

此错误弹出窗口:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`ltfrbr10infosystem`.`franchises`, CONSTRAINT `franchises_operator_id_foreign` FOREIGN KEY (`operator_id`) REFERENCES `operators` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
  

Laravel迁移:

public function up()
{
    Schema::create('franchises', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('operator_id')->nullable()->unsigned();
        $table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');

        $table->string('case_number')->nullable();
        $table->string('business_address')->nullable();
        $table->date('date_granted')->nullable();
        $table->date('expiry_date')->nullable();
        $table->string('route_name')->nullable();
        $table->string('deno')->nullable();
        $table->integer('authorize_units')->nullable();
        $table->string('remarks')->nullable();
        $table->timestamps();
    });
}

我已经尝试过了,但是仍然给我错误

$table->integer('operator_id')->nullable()->unsigned()->change();

我也尝试过

$table->integer('operator_id')->unsigned()->default(null);

如何使operator_id外键默认为null?

3 个答案:

答案 0 :(得分:0)

如果数据库中的数据不重要,则可以使用

刷新迁移和数据库。
  

php artisan migration:refresh

这将回滚并再次迁移所有迁移。确保您正确编写了down方法, 另外,您的迁移应如下所示:

public function up()
{
    Schema::create('franchises', function (Blueprint $table) {
        $table->increments('id');

        $table->unsignedInteger('operator_id')->nullable();
        $table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');

        $table->string('case_number')->nullable();
        $table->string('business_address')->nullable();
        $table->date('date_granted')->nullable();
        $table->date('expiry_date')->nullable();
        $table->string('route_name')->nullable();
        $table->string('deno')->nullable();
        $table->integer('authorize_units')->nullable();
        $table->string('remarks')->nullable();
        $table->timestamps();
    });
}

另一种实现方法是创建一个新的迁移,如下所示:

public function up()
{
    Schema::table('franchises', function (Blueprint $table) {

        $table->unsignedInteger('operator_id')->nullable()->change();


    });
}

答案 1 :(得分:0)

我认为您会收到此错误,因为您要插入的记录包含针对operator_id列的错误值。此值不是正确的运算符ID,也不为null(可以为0,“ null”或空字符串,...)

您可以在此处粘贴引起该错误的确切SQL查询吗?

答案 2 :(得分:0)

添加此内部函数并运行php artisan migrate:refresh --seed

public function up(){

    Schema::disableForeignKeyConstraints();
    Schema::create('franchises', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('operator_id')->unsigned()->nullable();
    $table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
    $table->string('case_number')->nullable();
    $table->string('business_address')->nullable();
    $table->date('date_granted')->nullable();
    $table->date('expiry_date')->nullable();
    $table->string('route_name')->nullable();
    $table->string('deno')->nullable();
    $table->integer('authorize_units')->nullable();
    $table->string('remarks')->nullable();
    $table->timestamps();
});Schema::enableForeignKeyConstraints();}