laravel 删除迁移中的默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38351498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:08:53  来源:igfitidea点击:

remove Default in migration?

laravellaravel-5.2

提问by I'll-Be-Back

In the migration I alter enabledfield to set to 1 value as default.

在迁移中,我将enabled字段更改为默认值设置为 1。

public function up()
{
    Schema::table('client', function (Blueprint $table) {
        $table->boolean('enabled')->default(1)->change();
    });
}

In down()method - How do I remove default()? I know can do default(0)but default was never created during create table.

down()方法中 - 如何删除default()?我知道可以,default(0)但在创建表期间从未创建默认值。

回答by x-yuri

Surprisingly or not, ->default(NULL)removes default value from a table:

无论是否令人惊讶,->default(NULL)从表中删除默认值:

public function up()
{
    Schema::table('client', function (Blueprint $table) {
        $table->boolean('enabled')->default(NULL)->change();
    });
}

Just omitting defaultcall doesn't work, since laravelmakes a diffbetween current stateand current state + specified changes. No changes specified, no diff.

只是省略default不能正常工作,因为laravel做一个差异之间的当前状态当前状态+指定的更改。没有指定更改,没有差异。

After that, doctrinegenerates ALTER TABLE statement(particularly, resulting column declaration), treating NULLvalueas default value not being specified.

之后,doctrine生成ALTER TABLE 语句(特别是结果列声明),将NULLvalue视为未指定的默认值。

回答by Julian Rodriguez

Since there is no way to remove this statement with Laravel functions, your down function must execute the statement as raw. It should be something like:

由于 Laravel 函数无法删除此语句,因此您的 down 函数必须将语句作为原始语句执行。它应该是这样的:

public function down()
  {
    Schema::table('client', function ($table) {
      DB::statement('ALTER TABLE' . $table . 'ALTER COLUMN enabled DROP DEFAULT');
    });
  }

In order to execute this migration, you need to incluide at the top of your migration:

为了执行此迁移,您需要在迁移的顶部包含:

use Illuminate\Support\Facades\DB;