【问题标题】:Alter ENUM column and add value to that column in Laravel在 Laravel 中更改 ENUM 列并向该列添加值
【发布时间】:2019-07-15 08:15:42
【问题描述】:

我有一个 MySQL 数据库,其中包含一个名为 user_level_attempt 的表。该表有一个 ENUM 类型的列,其中包含 ['PROGRESSED', 'STOPPED', 'COMPLETED'] 值。我需要编写迁移以向该列添加另一个值(比如说“通过”)。添加后,它看起来像这样,['PROGRESSED', 'STOPPED', 'COMPLETED', 'PASSED]。我怎么能在 Laravel 中做到这一点? 我尝试了以下解决方案,但它似乎不是一个好的做法/解决方案。

 /**
         * Schema table name to migrate
         * @var string
         */
        public $set_schema_table = 'bt_user_level_attempt';


        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table($this->set_schema_table, function ($table) {
                $table->dropColumn('status');
            });

            Schema::table($this->set_schema_table, function ($table) {
                $table->enum('status', ['PROGRESS', 'STOPPED', 'COMPLETED', 'PASSED'])->default('PROGRESS')->after('effective_time_spend');
            });
        }

/**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table($this->set_schema_table, function ($table) {
            $table->dropColumn('status');
        });

        Schema::table($this->set_schema_table, function ($table) {
            $table->enum('status', ['PROGRESS', 'STOPPED', 'COMPLETED'])->default('PROGRESS')->after('effective_time_spend');
        });
    }

谢谢。

【问题讨论】:

    标签: php laravel enums eloquent database-migration


    【解决方案1】:

    毕竟,我想办法找到解决办法。感谢所有给我启发的小伙伴。 :)

    /**
         * Schema table name to migrate
         * @var string
         */
        public $set_schema_table = 'bt_user_level_attempt';
    
    
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED', 'PASSED') NOT NULL DEFAULT 'PROGRESS'");
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED') NOT NULL DEFAULT 'PROGRESS'");
        }
    

    【讨论】:

      【解决方案2】:

      你应该试试DB::statement 方法:

      使用DB::statement 方法:

      DB::statement("ALTER TABLE ".$this->set_schema_table." CHANGE COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED','PASSED') NOT NULL DEFAULT 'PROGRESS'");
      

      【讨论】:

      • 更改不起作用。我尝试了修改而不是更改,并且成功了!
      • @NandunMalinda:好的,如果我的回答对你有用,那么请投票并接受我的回答
      • 你也可以对我的完整解决方案做同样的事情;)
      【解决方案3】:

      请参考文档:

      只有以下列类型可以“更改”:bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger 和 unsignedSmallInteger。

      因此无法使用简单的迁移语法修改 ENUM。但是您可以使用自定义语句迁移您的列:

      DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED','PASSED') NOT NULL DEFAULT 'PROGRESS'");
      

      【讨论】:

        【解决方案4】:

        将此代码添加到您的迁移文件中的架构之前。

        public function __construct()
            {
                \Illuminate\Support\Facades\DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
            }
        

        【讨论】:

          【解决方案5】:

          试试类似的东西:

          编辑

          $table->enum('converted', array('yes','no'))->default('no');
          

          【讨论】:

            猜你喜欢
            • 2013-03-16
            • 2010-12-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-27
            • 1970-01-01
            • 2023-03-29
            • 1970-01-01
            相关资源
            最近更新 更多