Rails迁移 - 带有类型转换的change_column

时间:2013-06-12 20:54:30

标签: ruby-on-rails ruby-on-rails-3

我已经谷歌了一下,似乎对我的问题没有令人满意的答案。

我有一个包含string类型列的表。 我想继续迁移:

class ChangeColumnToBoolean < ActiveRecord::Migration
    def up
        change_column :users, :smoking, :boolean
    end
end

当我运行此操作时,我收到以下错误

PG::Error: ERROR:  column "smoking" cannot be cast automatically to type boolean
HINT:  Specify a USING expression to perform the conversion.
: ALTER TABLE "users" ALTER COLUMN "smoking" TYPE boolean

我知道我可以使用纯SQL执行此迁移,但如果我可以使用Rails执行此操作仍然会更好。我通过了Rails代码,似乎没有这种可能性,但也许有人知道一种方式?

我对以下内容不感兴趣: - 纯SQL - 放下专栏 - 创建另一列,转换数据,删除原始,然后重命名

4 个答案:

答案 0 :(得分:107)

如果smoking列中的字符串已经是有效的布尔值,则以下语句将更改列类型而不会丢失数据:

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

同样,您可以使用此语句将列转换为整数:

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

我正在使用Postgres。不确定此解决方案是否适用于其他数据库。

答案 1 :(得分:34)

并非所有数据库都允许更改列类型,通常采用的方法是添加所需类型的新列,引入任何数据,删除旧列并重命名新列。

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

答案 2 :(得分:8)

对于postgres中的布尔值是正确的:

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

您可以在表达式中添加更多WHEN - THEN条件

对于其他数据库服务器,将根据数据库服务器的语法构造表达式,但原理是相同的。只有手动转换算法,完全没有SQL,遗憾的是还不够。

语法change_column :table, :filed, 'boolean USING CAST(field AS boolean)'的方法仅适用于字段内容如下:true / false / null

答案 3 :(得分:5)

由于我使用Postgres,我现在使用SQL解决方案。 使用的查询:

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

只有当一个字段填充了真/假字符串时才会起作用(例如生成强制布尔类型的默认单选按钮集合助手)