我的事件模型中有一个 currency(ccy) 列,它目前是一个字符串,我想将它更改为一个整数。
虽然它适用于我的本地环境,但当我尝试 heroku run rake db:migrate
到 heroku 时显示以下错误。
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: ""
: ALTER TABLE "events" ALTER COLUMN "ccy" TYPE integer USING CAST(ccy AS integer)
迁移文件如下。
class ChangeCcyToEvents < ActiveRecord::Migration
def change
change_column :events, :ccy, 'integer USING CAST(ccy AS integer)'
end
end
我发现了类似的问题 PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "M" ,所以我如下更改了迁移文件。但结果是一样的。
class ChangeCcyToEvents < ActiveRecord::Migration
def change
Event.where(ccy: '').update_all(ccy: '')
change_column :events, :ccy, 'integer USING CAST(ccy AS integer)'
end
end
目前所有ccy
列都没有值。
如果您能给我任何建议,我们将不胜感激。
最佳答案
您的迁移正在尝试将 ""
转换为整数,postgres 对此表示赞同(它应该这样做,因为它不是有效的转换)。
在更改列类型之前,您应该更新无效案例的内容:
class ChangeCcyToEvents < ActiveRecord::Migration
def up
Event.where("ccy IS NULL OR ccy = ''").update_all({ccy: '0'})
change_column :events, :ccy, 'integer USING CAST(ccy AS integer)'
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
也许在开发(本地主机)中没有提示,因为您使用的是与 Heroku 不同的 postgreSQL 版本。
下次尝试使用 default values在这种列的迁移中。
关于ruby-on-rails - heroku:PG::InvalidTextRepresentation:错误:整数的无效输入语法: “”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37767693/