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
remove Default in migration?
提问by I'll-Be-Back
In the migration I alter enabled
field 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 default
call doesn't work, since laravel
makes a diffbetween current stateand current state + specified changes. No changes specified, no diff.
只是省略default
不能正常工作,因为laravel
做一个差异之间的当前状态和当前状态+指定的更改。没有指定更改,没有差异。
After that, doctrine
generates ALTER TABLE statement(particularly, resulting column declaration), treating NULL
valueas default value not being specified.
之后,doctrine
生成ALTER TABLE 语句(特别是结果列声明),将NULL
value视为未指定的默认值。
回答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;