繁体   English   中英

heroku:PG :: InvalidTextRepresentation:错误:整数的无效输入语法:“”

[英]heroku: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “”

我的事件模型上有一个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:错误:整数:“ 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版本。

下次,请尝试在此类列的迁移中使用默认值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM