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

前端 未结 2 1575
醉酒成梦
醉酒成梦 2021-01-11 12:44

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?

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-11 13:09

    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"
    

提交回复
热议问题