![](/img/trans.png)
[英]PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer
[英]PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “aaa”
我遵循了類似問題的所有建議,但沒有一個解決了我的問題。 我想將字段gl_code
從string
更改為integer
,並嘗試了以下操作:
class ChangeCategoryGlCode < ActiveRecord::Migration
def change
# Category.all.each { |cat| cat.update(gl_code: cat.gl_code.to_i) }
Category.where("gl_code IS NULL OR gl_code = ''").update_all({gl_code: '0'})
# change_column :categories, :gl_code, :integer, using: 'gl_code::integer'
change_column :categories, :gl_code, 'integer USING CAST(gl_code AS integer)'
end
end
但是似乎沒有任何作用。 我什至ssh'd到服務器並手動運行命令,但是無論何時嘗試部署,它都會在rake db:migrate
失敗,並出現上述錯誤。
歡迎任何建議/提示。
編輯:如果那很重要,我正在使用Apartment gem,並嘗試更改每個租戶的Category
的gl_code
。
使用PL / pgSQL查找有罪的行:
DO
$$DECLARE
v_gl_code text;
v_id bigint;
BEGIN
FOR v_id, v_gl_code IN
SELECT id, gl_code FROM category
LOOP
BEGIN
PERFORM v_gl_code::integer;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'Bad value at id = %', v_id;
END;
END LOOP;
END;$$;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.