英文:
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');
});
//Rename the column
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论