我正在尝试使用新的 DataMapper 模型映射旧版 MySQL 数据库。
MySQL 架构:
CREATE TABLE IF NOT EXISTS `backer` (
`backer_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`secret` varchar(16) NOT NULL,
`email` varchar(255) DEFAULT NULL,
`status` enum('pending','ready') NOT NULL DEFAULT 'pending', # relevant bit
PRIMARY KEY (`backer_id`),
UNIQUE KEY `backer_id` (`secret`),
KEY `email` (`email`,`status`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8166 ;
数据映射器模型:
class Backer
include DataMapper::Resource
storage_names[:default] = "backer"
property :id, Serial, :field => "backer_id"
property :secret, String, :field => "secret"
property :email, String, :field => "email"
property :status, Enum[ :pending, :ready ], :field => "status", :default => "pending"
has n, :items, :child_key => "backer_id"
end
DataMapper.finalize
对于大多数属性,我可以坚持:
b = Backer.first
b.first_name = "Daniel"
b.save!
# Backer.first.first_name == "Daniel"
它仍然很好。但是当我这样做时enum
:
b = Backer.first
b.status = :pending
b.save!
# b.status == :pending
# Backer.first.status != :pending
# Backer.first.update!(:status => :pending) ...
对字段的更新/保存enum
似乎不会持续存在。
SQL 通常看起来像:
1.9.3p327 :019 > Backer.last.update(:status => :ready)
~ (0.000349) SELECT `backer_id`, `secret`, `email`, `status` FROM `backer` ORDER BY `backer_id` DESC LIMIT 1
~ (0.000190) UPDATE `backer` SET `status` = 2 WHERE `backer_id` = 8166
=> true
但是当我查看对象(例如Backer.last
)时status
,不会改变。
更新
我已经验证status
了 MySQL 数据库中的枚举字段正在被持久化。但是,status
DataMapper 类中的枚举属性根本不反映这一点(始终为nil
)。
这里发生了什么事?