Laravel将列类型更改为枚举类型。

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

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. //Rename the column
  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.html
匿名

发表评论

匿名网友

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

确定