上下文
想把price
數(shù)據(jù)類型由string
改為integer
class ChangePriceTypeInUsers < ActiveRecord::Migration[5.0]
def up
change_column :users, :price, :integer
end
def down
change_column :users, :price, :string
end
end
在本地執(zhí)行rails db:migrate
時沒出錯(奇怪),在Heroku執(zhí)行時出錯术浪。
線索
- 報錯
PG::DatatypeMismatch: ERROR: column "column_name" cannot be cast automatically to type integer HINT: Specify a USING expression to perform the conversion.
過程
谷歌搜column "column_name" cannot be cast automatically to type integer
领跛。
在StackOverflow找打一個合適的解決辦法胞此,原文在這里歼疮。
結(jié)果
原來是string
類型不能直接轉(zhuǎn)成integer
。缺乏數(shù)據(jù)庫知識自点。改后的代碼為
class ChangePriceTypeInUsers < ActiveRecord::Migration[5.0]
def up
change_column :users, :price, 'integer USING CAST(price AS integer)'
end
def down
change_column :users, :price, :string
end
end