Rails migrations - change_column with type conversion

后端 未结 4 1432
深忆病人
深忆病人 2020-12-04 17:36

I already google\'d aroung a little bit and seems there\'s no satisfying answer for my problem.

I have a table with column of type string. I\'d like to run following

相关标签:
4条回答
  • 2020-12-04 18:01

    Since I'm using Postgres, I went with SQL solution for now. Query used:

        execute 'ALTER TABLE "users" ALTER COLUMN "smoking" TYPE boolean USING CASE WHEN "flatshare"=\'true\' THEN \'t\'::boolean ELSE \'f\'::boolean END'
    

    It works only if one has a field filled with true/false strings (such as default radio button collection helper with forced boolean type would generate)

    0 讨论(0)
  • 2020-12-04 18:03

    If your strings in smoking column are already valid boolean values, the following statement will change the column type without losing data:

    change_column :users, :smoking, 'boolean USING CAST(smoking AS boolean)'
    

    Similarly, you can use this statement to cast columns to integer:

    change_column :table_name, :column_name, 'integer USING CAST(column_name AS integer)'
    

    I am using Postgres. Not sure whether this solution works for other databases.

    0 讨论(0)
  • 2020-12-04 18:20

    So right for boolean in postgres:

    change_column :table_name, :field,'boolean USING (CASE field WHEN \'your any string as true\' THEN \'t\'::boolean ELSE \'f\'::boolean END)'
    

    and you may add some more WHEN - THEN condition in your expression

    For other database servers, the expression will be constructed based on the syntax for your database server, but the principle is the same. Only manual conversion algorithm, entirely without SQL there is not enough unfortunately.

    Way with syntax change_column :table, :filed, 'boolean USING CAST(field AS boolean)' is suitable only if the contents of the field something like: true / false / null

    0 讨论(0)
  • 2020-12-04 18:21

    Not all databases allow changing of column type, the generally taken approach is to add a new column of the desired type, bring any data across, remove the old column and rename the new one.

    add_column :users, :smoking_tmp, :boolean
    
    User.reset_column_information # make the new column available to model methods
    User.all.each do |user|
      user.smoking_tmp = user.smoking == 1 ? true : false # If smoking was an int, for example
      user.save
    end
    
    # OR as an update all call, set a default of false on the new column then update all to true if appropriate.
    User.where(:smoking => 1).update_all(:smoking_tmp = true) 
    
    remove_column :users, :smoking
    rename_column :users, :smoking_tmp, :smoking
    
    0 讨论(0)
提交回复
热议问题