Rails migrations - change_column with type conversion

后端 未结 4 1439
深忆病人
深忆病人 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)

提交回复
热议问题