[英]heroku: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “”
I have a currency(ccy) column on my event model and it's currently a string, I'd like to change it to a integer. 我的事件模型上有一个currency(ccy)列,当前是一个字符串,我想将其更改为整数。
Although it works on my local environment, the following error was displayed when I try heroku run rake db:migrate
to heroku. 尽管它适用于我的本地环境,但是当我尝试使用
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)
The migration file as below. 迁移文件如下。
class ChangeCcyToEvents < ActiveRecord::Migration
def change
change_column :events, :ccy, 'integer USING CAST(ccy AS integer)'
end
end
I found the similar question PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "M" , so I changed migration file as below. 我发现了类似的问题PG :: InvalidTextRepresentation:错误:整数:“ M”的无效输入语法 ,所以我如下更改了迁移文件。 But the result is the same.
但是结果是一样的。
class ChangeCcyToEvents < ActiveRecord::Migration
def change
Event.where(ccy: '').update_all(ccy: '')
change_column :events, :ccy, 'integer USING CAST(ccy AS integer)'
end
end
There is no value in all ccy
column so far. 到目前为止,所有
ccy
列中都没有任何值。
It would be appreciated if you could give me any advice. 如果您能给我任何建议,将不胜感激。
Your migration is trying to convert a ""
into an integer, which postgres complais about it (and it should, because it's not a valid conversion). 您的迁移正在尝试将
""
转换为整数,postgres对此表示满意(并且应该这样做,因为这不是有效的转换)。
You should update your content for invalid cases before changing the column type: 在更改列类型之前,应更新无效内容的内容:
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
Perhaps is not complaining in development (localhost) because you're using a different version of postgreSQL than Heroku. 也许在开发(本地主机)中不会抱怨,因为您使用的是与Heroku不同的postgreSQL版本。
Next time, try using default values in migrations for this kind of columns. 下次,请尝试在此类列的迁移中使用默认值 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.