【问题标题】:Cannot rename date time column with Laravel Migrtions无法使用 Laravel 迁移重命名日期时间列
【发布时间】:2018-09-19 06:18:08
【问题描述】:

我想简单地重命名我的一列,尽管它一直在说

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'sendReminderCouple48' (SQL: ALTER TABLE sendIntakes CHANGE sendReminderCouple36 sendReminderCouple48 DATETIME DE  
  FAULT 'NULL') 

目前是sendReminderCouple36 - DATETIME - NULLABLE

我只想将其重命名为sendReminderCouple48

public function up()
{
    Schema::table('sendIntakes', function (Blueprint $table) {
        $table->renameColumn('sendReminderCouple36', 'sendReminderCouple48');
    });
}

public function down()
{
    Schema::table('sendIntakes', function (Blueprint $table) {
        $table->renameColumn('sendReminderCouple48', 'sendReminderCouple36');
    });
}

注意:我不想对我的配置文件进行任何严格的更改。

【问题讨论】:

  • 根据github.com/flarum/core/issues/1211,Dbal 2.7.0(和2.7.1)版本已经发布。我相信这应该可以解决问题,尚未对其进行测试。只是将它扔给仍在跟踪此问题并等待依赖关系得到修复的任何人。

标签: php mysql laravel laravel-5 migration


【解决方案1】:

我通过将类型更改为文本来修复它 - 重命名 - 然后返回日期时间

public function up()
{
    Schema::table('sendIntakes', function (Blueprint $table) {
        $table->text('sendReminderCouple36')->default(null)->nullable()->change();
    });

    Schema::table('sendIntakes', function (Blueprint $table) {
        $table->renameColumn('sendReminderCouple36', 'sendReminderCouple48');
    });

    Schema::table('sendIntakes', function (Blueprint $table) {
        $table->datetime('sendReminderCouple48')->default(null)->nullable()->change();
    });
}

    public function down()
    {
        Schema::table('sendIntakes', function (Blueprint $table) {
            $table->text('sendReminderCouple48')->default(null)->nullable()->change();
        });

        Schema::table('sendIntakes', function (Blueprint $table) {
            $table->renameColumn('sendReminderCouple48', 'sendReminderCouple36');
        });

        Schema::table('sendIntakes', function (Blueprint $table) {
            $table->datetime('sendReminderCouple36')->default(null)->nullable()->change();
        });
    }

【讨论】:

    【解决方案2】:

    这可能与 Laravel 框架上的issue #22050 有关。您是否将 MariaDB 用作数据库引擎?

    按照线程中的建议,您可以尝试在进行重命名调用之前更改列的默认值:

    Schema::table('products', function (Blueprint $table) {
        $table->string('name')->default(null)->change();
    });
    Schema::table('products', function (Blueprint $table) {
        $table->renameColumn('name', 'description');
    });
    Schema::table('products', function (Blueprint $table) {
        $table->string('description')->default(null)->change();
    });
    

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 2018-12-10
      • 2015-07-28
      • 1970-01-01
      • 2014-12-18
      • 2019-09-09
      • 2020-06-22
      • 2016-03-14
      • 2018-07-18
      相关资源
      最近更新 更多