簡體   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