Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove Default in migration?

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

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.

like image 482
I'll-Be-Back Avatar asked Jul 13 '16 12:07

I'll-Be-Back


People also ask

How do I change the default value in laravel?

In our case we are going to do second option - create new migration. If we roll back this migration, we need to remove default value from that field using migration and this is how to do it: Schema::table('photos', function (Blueprint $table) { $table->integer('order')->default(NULL)->change(); });


1 Answers

Surprisingly or not, for NOT NULL fields ->default(null) removes the default from a table:

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

Just omitting the default() part doesn't work, since laravel makes a diff between current state and current state + specified changes. No changes specified, no diff.

After that, doctrine generates ALTER TABLE statement, treating NULL as no default value.

With nullable fields though, from what I can see, doctrine doesn't let you simply drop the default. The only option is supposedly to make them NOT NULL:

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

Maybe with PostgreSQL you can get away without converting to NOT NULL, but that's to be confirmed.

like image 115
x-yuri Avatar answered Oct 08 '22 08:10

x-yuri