Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Migration for ID Column to Start at 1,000 and Autoincrement Up From There?

I'd like the ID's of my Order model to start at 1000, and count up autoincrementally from there.

Can this be done via migration?

like image 216
Jordan Feldstein Avatar asked Aug 22 '12 15:08

Jordan Feldstein


1 Answers

In your migration, after table has been created, update the sequence with something like this:

create_table :products do |t|
  t.string  :name
  # other stuff
end

# for Postgres
execute "SELECT setval('products_id_seq', 1000)"

# and for mysql ...
execute "ALTER TABLE products AUTO_INCREMENT = 1000"
like image 186
Serge Balyuk Avatar answered Oct 18 '22 13:10

Serge Balyuk