在模型中更新枚举定义

发布于 2025-02-07 08:30:03 字数 357 浏览 1 评论 0原文

我想知道当您更新模型中的枚举定义时,要考虑的效果和事物是什么?还是您应该避免做的事情? 如果我已经在桌上有一百万个记录,并想将定义从中更改为此。

class Post < ApplicationRecord
  enum :status, { published: 0, draft: 1, archived: 2 }
end
class Post < ApplicationRecord
  enum :status, { published: 0, draft: 1, edited: 2, deleted: 3 }
end

更一般地,从表中添加和删除列时应该考虑什么?

I was wondering what are the effects and the things to consider when you update an enum definition in your model? Or if this something that you should avoid to do?
What if I already have a million record on the table and want to change the definition from this to this.

class Post < ApplicationRecord
  enum :status, { published: 0, draft: 1, archived: 2 }
end
class Post < ApplicationRecord
  enum :status, { published: 0, draft: 1, edited: 2, deleted: 3 }
end

More generally, what should be considered when adding and deleting columns from a table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2025-02-14 08:30:03

该枚举在表上没有添加新列,它代表列的可能值。

如果最初,枚举是{发布:0,草稿:1,存档:2},最终将其更改为{发布:0,草稿:1,编辑:2,删除:3},这应该是一个大问题,因为您更改了值参考。

例如,如果首先您有一个post 存档 post(表示数据库中的status值为2,根据枚举)和现在是编辑,它将发生以下内容:

old:

@post.status #=> 'archived'
@post.archived? #=> true

new (我的意思是没有数据库的更新。它将发生如果您仅在示例中更改模型中的枚举):

@post.status #=> 'edited'
@post.archived? #=> false
@post.edited? #=> true

它更改了值,因为列数据(2)表示最初不再具有的值(:Acranived =&gt; 2 < /代码>)。

使用枚举时,您应始终在枚举定义中添加不同的值,因此代表不同的内容。

在您的示例中,我觉得可能是:

enum:statum,{发布:0,草稿:1,编辑:3,删除:4},因此它不引用旧值(<代码>:存档=&gt; 2 )。

通过这种方式,您有不同的参考文献,并且不会破坏它,您想在不久的将来添加存档

enum:状态,状态,{发布:0,草稿: 1,存档:2,编辑:3,删除:4}

The enum doesn't add new columns on the table, it represents a possible value to a column.

If initially the enum is { published: 0, draft: 1, archived: 2 } and it eventually changes to { published: 0, draft: 1, edited: 2, deleted: 3 }, it should be a big problem, because you changed the value reference.

For example, if at first you had a Post archived post (means the status value in the database is 2, according to the enum) and it is now edited, it will happen the following thing:

Old:

@post.status #=> 'archived'
@post.archived? #=> true

New (and I mean with no updates to the database. It will happen if you just change the enum in the model in the example you gave):

@post.status #=> 'edited'
@post.archived? #=> false
@post.edited? #=> true

It changed the value, because the column data (2) represents a value that is no longer what you had initially (:archived => 2).

When working with enums, you should always add different values in the enum definition, so it represents different things.

In your example, I feel it could be:

enum :status, { published: 0, draft: 1, edited: 3, deleted: 4 }, so it doesn't reference the old value (:archived => 2).

In this way, you have different references, and it also won't break it you want to add the archived back in a near future:

enum :status, { published: 0, draft: 1, archived: 2, edited: 3, deleted: 4 }

贪恋 2025-02-14 08:30:03

枚举不会在表中添加新列。它将在同一列中具有不同的值。例如,从您的新定义中,存档的帖子现在已编辑,也将有新的删除帖子。在数据库级别中,现在它将具有三个不同的值-0,1,2,3在同一列中。

枚举的另一个优点是,铁轨将为一些有用的帮助者与他们合作。

Enum will not add a new column in the table. It will just have different values in the same column. For example, from you new definition, the archived posts are now edited posts and there will be new deleted posts too. In the database level, now it will have three distinct values - 0,1,2,3 in the same column.

The other advantage of enum is that Rails will provide some helpful helpers to work with them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文