Search code examples
ruby-on-railsrails-migrations

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?


Solution

  • 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"