ruby-on-rails – Rails 4迁移将列数据类型从字符串更改为整数并
发布时间:2020-12-16 19:22:19 所属栏目:百科 来源:网络整理
导读:我需要将字符串字段转换为整数并使用枚举. 在不丢失数据的情况下,最好的方法是什么? 这是当前的迁移: class CreateSystems ActiveRecord::Migration def change create_table :systems do |t| t.string :operation t.string :status t.timestamps null: fa
我需要将字符串字段转换为整数并使用枚举.
在不丢失数据的情况下,最好的方法是什么? 这是当前的迁移: class CreateSystems < ActiveRecord::Migration def change create_table :systems do |t| t.string :operation t.string :status t.timestamps null: false end end end 然后我改变字段的类型,如下所示: class ChangeColumnsForSystems < ActiveRecord::Migration def change change_column :systems,:operation,:integer change_column :systems,:status,:integer end end 并更新模型文件. /app/models/system.rb ... enum operation { start: 0,stop: 1 } enum status { init: 0,working: 1,complete: 2 } ... 如何更新旧数据? 解决方法
经过一些研究,我发现这是一个合适的解决方案.
class ChangeColumnsForSystems < ActiveRecord::Migration def change change_column :systems,"integer USING (CASE operation WHEN 'start' THEN '0'::integer ELSE '1'::integer END)",null: false change_column :systems,"integer USING (CASE status WHEN 'init' THEN '0'::integer WHEN 'working' THEN '1'::integer ELSE '2'::integer END)",null: false end end 更新: class ChangeColumnsForSystems < ActiveRecord::Migration def up change_column_default :systems,nil change_column :systems,null: false,default: 0 end def down change_column_default :systems,"varchar USING (CASE operation WHEN '0' THEN 'start'::varchar ELSE 'stop'::varchar END)","varchar USING (CASE status WHEN '0' THEN 'init'::varchar WHEN '1' THEN 'working'::varchar ELSE 'complete'::varchar END)",default: 'init' end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |