Back

Rails Postgres add auto increment column

added on :: 12 August 2015
class AdddesignsId < ActiveRecord::Migration
def self.up
add_column :designs, :custom_id, :integer
execute <<-SQL
CREATE SEQUENCE custom_id_seq START 1;
ALTER SEQUENCE custom_id_seq OWNED BY designs.custom_id;
ALTER TABLE designs ALTER COLUMN custom_id SET DEFAULT nextval('custom_id_seq');
SQL
end


def self.down
remove_column :designs, :custom_id
execute <<-SQL
DROP SEQUENCE custom_id_seq;
SQL
end


end

 
Back