我在模型中定义了以下enum
字段:
class ClientRegistration < ApplicationRecord
enum status: { pending: 0, registered: 1, activated: 2, suspended: 3 }
enum cloud_type: { onprem: 0, aws: 1, gcp: 2, azure: 3 }
end
相同的迁移文件如下所示:
class CreateClientRegistrations < ActiveRecord::Migration[5.2]
def change
create_table :client_registrations do |t|
t.string :instance_id
t.string :private_ip
t.string :mac_address
t.integer :cloud_type, default: 0
t.string :public_ip
t.string :region
t.integer :status, default: 0
t.timestamps
end
end
end
现在奇怪的是,如果我未指定任何内容,则我的第一列状态没有得到它的默认值。另外,如果我明确指定为pending
,它也不会采用第一个值。但是它在第二列cloud_type
的情况下有效,如果我未指定任何默认值,它将默认为onprem
。我是否需要更改column_name左右?
答案 0 :(得分:1)
您可以尝试在迁移中添加“ null:false”。
t.integer :cloud_type, null: false, default: 0
t.integer :status, null: false, default: 0