1
我試圖用新的DataMapper模型映射舊版MySQL數據庫。DataMapper枚舉字段在保存或更新時不會保留
的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 ;
DataMapper的模式:
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
不被改變。
UPDATE
我已經驗證,在MySQL數據庫中列舉status
領域正在被持續。但是,DataMapper類中枚舉的status
屬性根本不反映(始終爲nil
)。
這裏發生了什麼?
'b.reload'ing幫助嗎? – morbusg
啊,不用等待,DataMapper的'Enum'持續爲'Integer',而MySQL' enum'則是一個字符串。所以你很可能需要修補'DataMapper :: Property :: Enum'來支持MySQL'enum'。 – morbusg