【问题标题】:Rails. Migration enum with "t.cloumn..." or "t.integer"?导轨。使用“t.cloumn ...”或“t.integer”迁移枚举?
【发布时间】:2017-05-06 21:57:23
【问题描述】:

有时在迁移文件中我看到:

t.column :model_type, :integer, default: 0, null: false

有时:

t.integer :model_type, default: 0, null: false

对于枚举类型

哪种方法是正确的?

【问题讨论】:

  • 这两种方式是一样的
  • @IlyaLavrov 你确定吗?

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-5


【解决方案1】:

让我们以两种方式向表中添加列:

t.column :model_type1, :integer, default: 0, null: false
t.integer :model_type2, default: 0, null: false

现在我们可以检查一下 Rails 是如何在schema.rb 中存储这两列的:

t.integer "model_type1", default: 0, null: false
t.integer "model_type2", default: 0, null: false

我们还可以检查数据库中的表结构(在我的例子中是 PostgeSQL):

model_type1 | integer | not null default 0
model_type2 | integer | not null default 0

如您所见,两种方式的结果相同。

【讨论】:

    猜你喜欢
    • 2022-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 2023-03-07
    • 2014-10-04
    • 2015-09-01
    • 1970-01-01
    相关资源
    最近更新 更多