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

爱⌒轻易说出口 提交于 2019-12-19 05:17:26

问题


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?


回答1:


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"



回答2:


This has not been tested and I am not sure what db you are using.

create_table(:order, :id => false) do |t|
   t.integer :id, :options => 'PRIMARY KEY', :default => 1000

or if you already have the table try this migration

def change
  execute "ALTER TABLE orders AUTO_INCREMENT = 1000"
end


来源:https://stackoverflow.com/questions/12076741/rails-migration-for-id-column-to-start-at-1-000-and-autoincrement-up-from-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!