I have a table that contains an enum field
CREATE TABLE `user_status` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`values` enum(\'on\', \'off\'),
I did it with MySql:
class ChangeJobTypeEnum extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement("ALTER TABLE _TABLENAME_ CHANGE _COLUMNNAME_ _COLUMNNAME_ ENUM('on', 'off', 'auto')");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement("ALTER TABLE _TABLENAME_ CHANGE _COLUMNNAME_ _COLUMNNAME_ ENUM('on', 'off')");
}
}
I had a slightly different situation, it was necessary to add new items, change existing and remove old. This is my example.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeEnum extends Migration
{
public function up()
{
Schema::table('table_example', function (Blueprint $table) {
DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third', 'fourth', 'fifth', 'sixth') NOT NULL;");
DB::statement("UPDATE `field` set `status` = 'fourth' where `status` = 'first';");
DB::statement("UPDATE `field` set `status` = 'fifth' where `status` = 'second';");
DB::statement("ALTER TABLE table_example MODIFY status enum('third', 'fourth', 'fifth', 'sixth') NOT NULL;");
});
}
public function down()
{
Schema::table('table_example', function (Blueprint $table) {
DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third', 'fourth', 'fifth', 'sixth') NOT NULL;");
DB::statement("UPDATE `field` set `status` = 'first' where `status` = 'fourth';");
DB::statement("UPDATE `field` set `status` = 'second' where `status` = 'fifth';");
DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third',) NOT NULL;");
});
}
}
By the way, generate row SQL query via JetBrains ide(DataGrip), is like that:
∧_∧
(。・ω・。)つ━☆・*。
⊂ ノ ・゜+.
しーJ °。+ *´¨)
I say
public function up()
{
Schema::create('dt_warehouses', function (Blueprint $table) {
**$table->enum('isactive', ['Y', 'N'])->default('Y');**
$table->timestamps();
});
}
Laravel doesn't provide methods to update an enum column. You can delete and recreate the column but you could loose data during the operation and it's not really clean.
In this case, I think that the best choice is to write raw SQL into a migration :
public function up()
{
DB::statement("ALTER TABLE user_status MODIFY COLUMN ENUM('on','off','unknown')");
}
public function down()
{
DB::statement("ALTER TABLE user_status MODIFY COLUMN ENUM('on','off')");
}
I could have made a mistake in the SQL syntax, I have never used ENUM
, but you can see the idea anyway.
The second answer works, but in my case CHANGE
was throwing an error. So i tried using MODIFY
instead and that worked. Thanks guys..
Here is my code:
class ChangeJobTypeEnum extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement("ALTER TABLE _TABLENAME_ MODIFY _COLUMNNAME_ ENUM('on', 'off', 'auto')");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement("ALTER TABLE _TABLENAME_ MODIFY_COLUMNNAME_ ENUM('on', 'off')");
}
}