有时,rails修改字段类型的时候,会报一个不能转换的错误,比如varchar转float
错误代码如下:
PG::DatatypeMismatch: ERROR: column "traffic_total" cannot be cast automatically to type double
HINT: Specify a USING expression to perform the conversion.
这个时候有两种解决办法:
1.推荐做法
def change
change_column :sim_cards, :traffic_total, 'double precision USING CAST(traffic_total AS double precision)'
end
2.删除字段,再添加
def change
remove_column :sim_cards, :traffic_total
add_column :sim_cards, :traffic_total, :float
end
推荐第一种,不会删除数据。