Laravel 将列类型更改为枚举

huangapple go评论79阅读模式
英文:

Laravel changing column type to enum

问题

我正在尝试将一个varchar列更改为enum类型。我编写并运行了我的迁移 - 创建新列,设置值的逻辑,删除旧列,重命名新列。起初我以为一切都很顺利,但仔细检查后,列仍然是varchar而不是enum,但值已经(正确地)更改。

我回滚了,然后只运行了前两个步骤,添加enum列并设置值,但没有删除/重命名,新列被正确地添加为enum。

为什么这不按预期工作?

以下是我的迁移文件中的"up"部分:

public function up()
{
    Schema::table('lifts', function (Blueprint $table) {
        $table->enum('lift_type', ['magic_carpet', 'rope_tow', 'platter', 't_bar','j_bar', 'fixed_chair', 'express_chair', 'bubble_chair', 'gondola', 'chondola', 'tram', 'unknown'])->default('unknown')->after('type');
    });

    $lifts = Lift::all();
    $lifts->each(function ($lift) {
        switch ($lift->type) {
            case "Bubble Chair":
                $lift->lift_type = "bubble_chair";
                break;
            // 其他情况...
            default:
                $lift->lift_type = "unknown";
                break;
        }
        $lift->save();
    });

    Schema::table('lifts', function (Blueprint $table) {
        $table->dropColumn('type');
    });

    // 重命名列

    Schema::table('lifts', function (Blueprint $table) {
        $table->renameColumn('lift_type', 'type');
    });
}

希望这能帮助您解决问题。

英文:

I am trying to change a varchar column to an enum type. I wrote and ran my migration - create new column, logic to set the values, drop old column, rename new column. At first thought it all went well, but on closer inspection, the column was still a varchar not an enum, but the values had been (correctly) changed.

I rolled back, and then just ran the first 2 steps, adding the enum column and setting the values, but not dropping/renaming and the new column was correctly added as an enum.

Why is this not working as expected?

Here's my up:

    public function up()
    {
        Schema::table('lifts', function (Blueprint $table) {

            $table->enum('lift_type', ['magic_carpet', 'rope_tow', 'platter', 't_bar','j_bar', 'fixed_chair', 'express_chair', 'bubble_chair', 'gondola', 'chondola', 'tram', 'unknown'])->default('unknown')->after('type');
        });

        $lifts = Lift::all();
        $lifts->each(function ($lift) {
            switch ($lift->type) {
                case "Bubble Chair":
                    $lift->lift_type = "bubble_chair";
                    break;
              ...
                default:
                    $lift->lift_type = "unknown";
                    break;
                }
            $lift->save();
        });

        Schema::table('lifts', function (Blueprint $table) {

            $table->dropColumn('type');
        });

        //Rename the column

        Schema::table('lifts', function (Blueprint $table) {
            $table->renameColumn('lift_type', 'type');
        });
    }

答案1

得分: 0

你应该在定义枚举类型时包含 change 方法。

示例:

Schema::table('lifts', function (Blueprint $table) {

    $table->enum('lift_type', ['magic_carpet', 'rope_tow', 'platter', 
    't_bar','j_bar', 'fixed_chair', 'express_chair', 'bubble_chair', 
    'gondola', 'chondola', 'tram', 'unknown'])
    ->default('unknown')
    ->after('type')
    ->change(); //添加这一行;
});
英文:

You should include change method while defining enum type.

Example

     Schema::table('lifts', function (Blueprint $table) {

        $table->enum('lift_type', ['magic_carpet', 'rope_tow', 'platter', 
        't_bar','j_bar', 'fixed_chair', 'express_chair', 'bubble_chair', 
        'gondola', 'chondola', 'tram', 'unknown'])
        ->default('unknown')
        ->after('type')
        ->change() //add this;
     });

答案2

得分: 0

对于其他人遇到这个问题的情况,事实证明你不能重命名枚举列(文档中有提到,但我第一次看漏了!)

解决方案是首先重命名(为type_old),然后使用正确的名称添加枚举(然后分配正确的值,然后删除重命名的列)。

英文:

For anyone else who comes across this, it turns out you can't rename an enum column (it is in the docs, but I missed it the first time!)

The solution was to rename first (to type_old), then add the enum with the correct name (then assign correct values, then delete the renamed column)

huangapple
  • 本文由 发表于 2023年8月9日 11:14:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864337-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定