I have followed all suggestions from similar questions but none has solved my problem. I want to change the field gl_code
from string
to integer
and have tried the following:
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
But nothing seems to work. I even ssh'd to the server and run the commands manually but whenever I try to deploy it fails at rake db:migrate
with the error above.
Any suggestions/hints are welcome.
Edit: If that matters, I am using the Apartment gem, and have tried changing the gl_code
for Category
for each tenant.
Use PL/pgSQL to find the guilty row:
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;$$;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.