![](/img/trans.png)
[英]Rails3: Postgres Hstore: How to insert multiple records in a table that has an hstore column
[英]How to rename hstore key in Rails + Postgres
如何使用Postgres在Rails 4中重命名hstore密鑰?
User.update_all('hstorename -> oldcolumname: newcolumnname') ???
基於https://stackoverflow.com/a/13276545/305019
def change_hstore_key(klass, h, from_key, to_key)
klass.where("#{h} ? '#{from_key}'")
.update_all("#{h} = (#{h} - '#{from_key}'::text) || " \
"hstore('#{to_key}'::text, #{h} -> '#{from_key}')")
end
# usage
change_hstore_key(User, hstorename, oldcolumnname, newcolumnname)
重要說明 :請勿將其與任何用戶提供的參數一起使用,以避免SQL注入。 對於提供所有數據的遷移等,這應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.