heroku:PG :: InvalidTextRepresentation:错误:整数的输入语法无效:“” [英] heroku: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “”

查看:2454
本文介绍了heroku:PG :: InvalidTextRepresentation:错误:整数的输入语法无效:“”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的事件模型中有一个货币(ccy)列,它现在是一个字符串,我想将其更改为一个整数。



尽管它有效在我的本地环境中,当我尝试 heroku运行rake db:migrate 到heroku时显示以下错误。

  rake中止! 
StandardError:发生错误,本次以及所有后来的迁移都被取消:

PG :: InvalidTextRepresentation:错误:整数无效输入语法:
:ALTER TABLE事件ALTER COLUMNccyTYPE 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:错误:无效的整数输入语法:M,所以我改变了迁移文件如下。但结果是一样的。

  class ChangeCcyToEvents< ActiveRecord :: Migration 
def change
Event.where(ccy:'').update_all(ccy:'')
change_column:events,:ccy,'USING CAST(ccy AS integer )'
end
end

所有<$ c都没有值$ c> ccy 列。



如果您能给我任何建议,我们将不胜感激。


您的迁移正试图将转换为一个整数,postgres对此抱怨(和它应该,因为它不是一个有效的转换)。



您应该在更改列类型之前更新您的内容以查找无效情况:

  class ChangeCcyToEvents< ActiveRecord :: Migration 
def up
Event.where(ccy IS NULL or ccy ='').update_all({ccy:'0'})
change_column:events,:ccy ,'整数USING CAST(ccy AS整数)'
结束

def down
提高ActiveRecord :: IrreversibleMigration
结束
结束

或许你并没有抱怨在开发中(localhost),因为你使用的是不同版本的postgreSQL比Heroku。



下次尝试使用默认值在迁移这种列。


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.

Although it works on my local environment, the following error was displayed when I try heroku run rake db:migrate to 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. 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.

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).

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.

Next time, try using default values in migrations for this kind of columns.

这篇关于heroku:PG :: InvalidTextRepresentation:错误:整数的输入语法无效:“”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆