【问题标题】:Error NULL DEFAULT NULL Laravel migration with MariaDB使用 MariaDB 进行错误 NULL DEFAULT NULL Laravel 迁移
【发布时间】:2018-09-18 07:19:26
【问题描述】:

我有以下迁移,我在表中添加了一个额外的列:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddTenantIdToPeopleTable extends Migration
{
    public function up()
    {
        /*
        * Need to create the column as null and then mark it as non-null to
        * avoid SQLite problem General error: 1 Can not add to NOT NULL column with default value NULL
        */

        Schema::table('people', function (Blueprint $table) {
            $table->integer('tenant_id')->nullable()->unsigned();
            $table->index('tenant_id');
        });

        Schema::table('people', function (Blueprint $table) {
            $table->integer('tenant_id')->nullable(false)->change();
        });
    }

    public function down()
    {
        Schema::disableForeignKeyConstraints();
            Schema::table('people', function (Blueprint $table) {
                $table->dropColumn('tenant_id');
            });
        Schema::enableForeignKeyConstraints();
    }
}

我可以使用 PostgreSQL、SQLite 和 MySQL 运行此迁移而不会出现任何错误,但是当我尝试使用 Maria DB 运行时,会发生此错误:

Error

我尝试了一些东西,但没有任何效果。

【问题讨论】:

  • 您已经在 NULL 列中获得了现有数据。如果您没有 nullable 列,Maria DB 不想要 NULL 值。有什么理由不希望它可以为空?
  • 没有特别的原因。我设法用 postgres、mysql 和 sqlite 做到这一点,但 mariaDB 让我很难过。有什么办法可以做到吗?

标签: mysql laravel laravel-5 migration mariadb


【解决方案1】:

我已经设法让这件事发挥作用。 我刚刚添加了“->default(null)”

我这样做了:

public function up()
    {
        Schema::table('people', function (Blueprint $table) {
            $table->integer('tenant_id')->nullable()->unsigned();
            $table->index('tenant_id');
        });

        Schema::table('people', function (Blueprint $table) {
            $table->integer('tenant_id')->nullable(false)->default(null)->change();
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 2016-08-05
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2012-09-03
    相关资源
    最近更新 更多