我更改了我的数据库中的列:
class ChangeTestTypeInScores < ActiveRecord::Migration
def self.up
change_column :scores, :test_type, :boolean
end
def self.down
change_column :scores, :test_type, :string
end
end
它工作正常,但是当我推送到heroku并且Heroku运行rake db:migrate时,我收到以下错误:
PG::DatatypeMismatch: ERROR: column "test_type" cannot be cast automatically to type boolean
HINT: Specify a USING expression to perform the conversion.
: ALTER TABLE "scores" ALTER COLUMN "test_type" TYPE boolean
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
如果我已更新本地数据库,我将如何纠正此问题?
答案 0 :(得分:2)
我认为问题是,您已经在Heroku数据库中拥有test_type
的一些string
值,这些值为boolean
,且无法更改为change_column :scores, :test_type, 'boolean USING CAST(test_type AS boolean)'
。
有两种方法可以解决这个问题。
首先:将迁移更改为:
rake db:migrate
并运行{{1}}。
第二个(我更喜欢这一个)是删除Heroku数据库并再次运行迁移(以你在开始时编写的形式,因为不需要投射任何东西)。