【问题标题】:how can I create a migration to add a value to an enum in eloquent如何创建迁移以在 eloquent 中为枚举添加值
【发布时间】:2014-03-19 06:57:05
【问题描述】:

我有一个包含枚举字段的表

CREATE TABLE `user_status` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `values` enum('on', 'off'),
  PRIMARY KEY (`id`),
) ENGINE=InnoDB;

如何创建迁移以向枚举字段添加值?

【问题讨论】:

标签: laravel enums eloquent


【解决方案1】:

Laravel 不提供更新枚举列的方法。您可以删除并重新创建该列,但您可能会在操作过程中丢失数据,而且它并不是真正的干净

在这种情况下,我认为最好的选择是将原始 SQL 写入迁移:

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')");
}

我可能在 SQL 语法上犯了一个错误,我从未使用过ENUM,但无论如何你可以看到这个想法。

【讨论】:

  • 请注意,您只能使用此解决方案支持支持 ENUM 类型的数据库。例如,使用 SQLite 运行测试会中断。
  • 如果您不知道迁移将在哪个数据库平台上运行,它仍然是编写原始 SQL DDL 的“最佳”方式吗?
  • 有任何迁移名称约定吗?
  • 谢谢。我将列名放在COLUMN values 部分导致语法错误,您可以将其删除。
  • 只是添加一个建议,以防其他人面临我面临的问题:如果您尝试此解决方案并看到“SQLSTATE[42000]:语法错误或访问冲突:1064 你您的 SQL 语法有错误;" 您的问题可能是您尝试更新的枚举列的列名是众多 MySQL 关键字和保留字 之一(更多here)
【解决方案2】:

我是用 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')");

    }
}

【讨论】:

    【解决方案3】:

    我的情况略有不同,有必要添加新项目,更改现有项目并删除旧项目。这是我的例子。

     <?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;");
            });
        }
    }
    

    顺便说一句,通过JetBrains ide(DataGrip)生成行SQL查询,是这样的:

     ∧_∧ 
    (。・ω・。)つ━☆・*。
    ⊂   ノ    ・゜+.
     しーJ   °。+ *´¨)
    

    【讨论】:

      【解决方案4】:

      第二个答案有效,但在我的情况下,CHANGE 抛出了一个错误。所以我尝试改用MODIFY 并且效果很好。谢谢各位。。

      这是我的代码:

      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')");
      
      }
      }
      

      【讨论】:

        【解决方案5】:

        我说

        public function up()
            {
                Schema::create('dt_warehouses', function (Blueprint $table) {
                    **$table->enum('isactive', ['Y', 'N'])->default('Y');**
                    $table->timestamps();
                });
            }
        

        【讨论】:

          猜你喜欢
          • 2018-10-18
          • 2019-12-08
          • 2020-03-13
          • 2019-08-16
          • 2010-10-16
          • 1970-01-01
          • 2023-03-07
          • 2020-05-27
          • 1970-01-01
          相关资源
          最近更新 更多