Pie*_*ois 4 postgresql activerecord hstore ruby-on-rails-4
我目前有以下数据库模式:
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
Run Code Online (Sandbox Code Playgroud)
而且我想将这个payload领域迁移json到hstore.
执行以下操作:
change_column :user_actions, :payload, :hstore
Run Code Online (Sandbox Code Playgroud)
导致以下错误消息:
PG::DatatypeMismatch: ERROR: column "payload" cannot be cast automatically to type hstore
HINT: Specify a USING expression to perform the conversion.
Run Code Online (Sandbox Code Playgroud)
不知道如何使用USING提示以及在不丢失任何数据的情况下执行此迁移的最佳方法是什么?
Tai*_*aiz 12
提示: 指定USING表达式以执行转换
实际上格式是:
change_column :user_actions, :payload, '[type_to_which_you_want_to_change] USING CAST(data AS [type_to_which_you_want_to_change])'
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下:
change_column :user_actions, :payload, 'hstore USING CAST(payload AS hstore)'
Run Code Online (Sandbox Code Playgroud)
参考:
| 归档时间: |
|
| 查看次数: |
3954 次 |
| 最近记录: |