Activerecord brainspec/enumerize为默认值抛出mysql错误
我正在开发Rails 4应用程序,并开始使用brainspec/enumerize gem。我在数据库的Activerecord brainspec/enumerize为默认值抛出mysql错误,activerecord,ruby-on-rails-4,enumeration,Activerecord,Ruby On Rails 4,Enumeration,我正在开发Rails 4应用程序,并开始使用brainspec/enumerize gem。我在数据库的列status中有一个整数值,我想在我的模型中枚举它 下面您可以看到我用来设置它的代码段。不幸的是,在我的测试(之前都通过了)中,它抱怨由于无法保存行而创建了合作伙伴。无法将默认状态分配为NULL。不确定从何处获取NULL,因为数据库本身(MySQL)设置为默认值0,从下面可以看到,后端指示默认值4或:complete enumerize :status, in: [ :pending
列status
中有一个整数值,我想在我的模型中枚举它
下面您可以看到我用来设置它的代码段。不幸的是,在我的测试(之前都通过了)中,它抱怨由于无法保存行而创建了合作伙伴。无法将默认状态分配为NULL
。不确定从何处获取NULL
,因为数据库本身(MySQL)设置为默认值0
,从下面可以看到,后端指示默认值4
或:complete
enumerize :status, in: [
:pending, # 0 account has a pending billing request (but is not yet open)
:active, # 1 account has an active base subscription
:suspended, # 2 account has been suspended (e.g. after a base subscription decline)
:expired, # 3 base subscription has expired
:incomplete, # 4 partner application process incomplete
:closed, # 5 account has been permanently closed
:cancelled # 6 account has been cancelled by user (but is still unexpired)
], default: :incomplete
下面是ActiveRecord/MySQL错误
PartnerTest#test_create_with_nested_attributes:
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'status' cannot be null: UPDATE `partner` SET `primary_contact_id` = 3, `status` = NULL WHERE `partner`.`id` = 3
test/models/partner_test.rb:9:in `block in <class:PartnerTest>'
根据brainspec/enumerize自述文件,您应该为每个状态提供一个整数值,如:
enumerize :status, in: {
pending: 0, # 0 account has a pending billing request (but is not yet open)
active: 1, # 1 account has an active base subscription
suspended: 2, # 2 account has been suspended (e.g. after a base subscription decline)
expired: 3, # 3 base subscription has expired
incomplete: 4 # 4 partner application process incomplete
# And so on...
}, default: :incomplete
因为您只提供了键而没有提供值,所以它将其设置为nil/NULL
enumerize :status, in: {
pending: 0, # 0 account has a pending billing request (but is not yet open)
active: 1, # 1 account has an active base subscription
suspended: 2, # 2 account has been suspended (e.g. after a base subscription decline)
expired: 3, # 3 base subscription has expired
incomplete: 4 # 4 partner application process incomplete
# And so on...
}, default: :incomplete