Laravel 将列类型更改为枚举

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

Laravel changing column type to enum

问题

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

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

为什么这不按预期工作?

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

  1. public function up()
  2. {
  3. Schema::table('lifts', function (Blueprint $table) {
  4. $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');
  5. });
  6. $lifts = Lift::all();
  7. $lifts->each(function ($lift) {
  8. switch ($lift->type) {
  9. case "Bubble Chair":
  10. $lift->lift_type = "bubble_chair";
  11. break;
  12. // 其他情况...
  13. default:
  14. $lift->lift_type = "unknown";
  15. break;
  16. }
  17. $lift->save();
  18. });
  19. Schema::table('lifts', function (Blueprint $table) {
  20. $table->dropColumn('type');
  21. });
  22. // 重命名列
  23. Schema::table('lifts', function (Blueprint $table) {
  24. $table->renameColumn('lift_type', 'type');
  25. });
  26. }

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

英文:

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:

  1. public function up()
  2. {
  3. Schema::table('lifts', function (Blueprint $table) {
  4. $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');
  5. });
  6. $lifts = Lift::all();
  7. $lifts->each(function ($lift) {
  8. switch ($lift->type) {
  9. case "Bubble Chair":
  10. $lift->lift_type = "bubble_chair";
  11. break;
  12. ...
  13. default:
  14. $lift->lift_type = "unknown";
  15. break;
  16. }
  17. $lift->save();
  18. });
  19. Schema::table('lifts', function (Blueprint $table) {
  20. $table->dropColumn('type');
  21. });
  22. //Rename the column
  23. Schema::table('lifts', function (Blueprint $table) {
  24. $table->renameColumn('lift_type', 'type');
  25. });
  26. }

答案1

得分: 0

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

示例:

  1. Schema::table('lifts', function (Blueprint $table) {
  2. $table->enum('lift_type', ['magic_carpet', 'rope_tow', 'platter',
  3. 't_bar','j_bar', 'fixed_chair', 'express_chair', 'bubble_chair',
  4. 'gondola', 'chondola', 'tram', 'unknown'])
  5. ->default('unknown')
  6. ->after('type')
  7. ->change(); //添加这一行;
  8. });
英文:

You should include change method while defining enum type.

Example

  1. Schema::table('lifts', function (Blueprint $table) {
  2. $table->enum('lift_type', ['magic_carpet', 'rope_tow', 'platter',
  3. 't_bar','j_bar', 'fixed_chair', 'express_chair', 'bubble_chair',
  4. 'gondola', 'chondola', 'tram', 'unknown'])
  5. ->default('unknown')
  6. ->after('type')
  7. ->change() //add this;
  8. });

答案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:

确定