【发布时间】:2017-10-23 05:45:26
【问题描述】:
我需要将 DB 中列的数据类型从 :string 更改为 :decimal。所有值都采用以下格式:"129.99",因此转换它们不应引发任何错误。
为此,我编写了以下迁移:
def change
change_column :my_table, :target_column, :decimal
end
当我执行此操作时,它会显示以下错误以及如何修复它的提示:
PG::DatatypeMismatch: ERROR: column "target_column" cannot be cast automatically to type numeric
HINT: You might need to specify "USING target_column::numeric".
但是,我似乎找不到任何有关如何执行此操作的文档,因此该提示并没有真正帮助我。
执行此迁移的最佳方法是什么?
【问题讨论】:
-
试试
change_column :my_table, :target_column, 'numeric USING CAST(target_column AS numeric)' -
@VaoTsun 工作,谢谢。如果你写一个正确的答案,我会接受。
标签: ruby-on-rails postgresql activerecord type-conversion