目前,我有以下DB模式:遷移JSON列類型HSTORE列類型
create_table :user_actions do |t|
t.integer :emitter_id
t.string :emitter_type
t.integer :target_id
t.string :target_type
t.json :payload
t.timestamps
end
而且我想給場遷移從json
到hstore
。
執行以下操作:
以下錯誤消息change_column :user_actions, :payload, :hstore
結果:
PG::DatatypeMismatch: ERROR: column "payload" cannot be cast automatically to type hstore
HINT: Specify a USING expression to perform the conversion.
不知道如何使用USING
提示,什麼是應該做這種遷移不會丟失任何的最佳方式數據?
確實[這個例子](http://www.postgresql.org/docs /9.3/static/hstore.html#AEN149208)有幫助嗎? (F.16.6。兼容性) – DrColossos
這可能是可行的@DrColossos示例顯示了postgres using子句。我不太瞭解ruby/activerecord,我確實發現了這個線程:https://github.com/rails/rails/issues/11062它表明1)你不能通過activerecord和2獲得使用條款)解決方法是直接到SQL。 – Greg
Thx,實際上去了一個簡單的解決方案,重命名列並做一個簡單的遷移'o.new_field = o.payload'其中'new_field'是一個'hstore' –