Change the default ActiveRecord ID from 0 to 1,000,000

I would like to change the minimum for the id of the created objects from 1 to 1000. Therefore, when I create my first model object in rails, it gets the identifier 1000, not 1.

Is there any way to install this in schema / migration files?

+4
source share
2 answers

I am not familiar with MySQL, but for Postgres you can do something similar in your migration file:

class CreateCustomers < ActiveRecord::Migration def self.up create_table :customers do |t| t.string :name t.timestamps end execute "SELECT setval('customers_id_seq', 1000);" end 

Note that the call to the execution method is performed outside the create_table block.

+5
source

For MySQL use

 class CreateCustomers < ActiveRecord::Migration def self.up create_table :customers do |t| t.string :name t.timestamps end execute "ALTER TABLE customers AUTO_INCREMENT = 1000;" end 

This will cause your auto_increment (id) field to start at 1000.

+5
source

Source: https://habr.com/ru/post/1303783/


All Articles