how can I create a migration to add a value to an enum in eloquent

后端 未结 5 1543
后悔当初
后悔当初 2020-12-28 14:46

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\'),
           


        
相关标签:
5条回答
  • 2020-12-28 14:53

    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')");
    
        }
    }
    
    0 讨论(0)
  • 2020-12-28 14:56

    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   °。+ *´¨)
    
    0 讨论(0)
  • 2020-12-28 14:56

    I say

    public function up()
        {
            Schema::create('dt_warehouses', function (Blueprint $table) {
                **$table->enum('isactive', ['Y', 'N'])->default('Y');**
                $table->timestamps();
            });
        }
    
    0 讨论(0)
  • 2020-12-28 15:06

    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.

    0 讨论(0)
  • 2020-12-28 15:13

    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')");
    
    }
    }
    
    0 讨论(0)
提交回复
热议问题