ID 列的 Rails 迁移从 1,000 开始并从那里自动递增? [英] Rails Migration for ID Column to Start at 1,000 and Autoincrement Up From There?
本文介绍了ID 列的 Rails 迁移从 1,000 开始并从那里自动递增?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的订单模型的 ID 从 1000 开始,并从那里自动递增.
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?
推荐答案
在您的迁移中,在创建表后,使用以下内容更新序列:
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"
这篇关于ID 列的 Rails 迁移从 1,000 开始并从那里自动递增?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文