我已经遵循了类似问题的所有建议,但没有一个解决了我的问题。
我想将字段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到服务器并手动运行命令,但每当我尝试部署它时,rake db:migrate
上的错误都会失败并出现上述错误。
欢迎任何建议/提示。
编辑:如果重要,我使用的是公寓宝石,并尝试为每个租户更改gl_code
Category
。
答案 0 :(得分:0)
使用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;$$;