Setting initial value for primary key in Rails (i.e. first object's id is 1000 instead of 1)

ruby-on-rails

Solution

This is database specific. Just do something like the following in your migration:

class MyMigration < ActiveRecord::Migration
  def self.up
    create_table :my_table do |t|
      # ...
    end
    execute "ALTER TABLE my_table AUTO_INCREMENT = 1000" # for MySQL
  end

  def self.down
    # ...
  end
end

Or indeed, even better, as suggested by Bane:

  def self.up
    create_table :my_table, :options => "AUTO_INCREMENT = 1000" do |t|
      # ...
    end
  end

Be cautious with database-specific migrations, though! Using any SQL that is specific to your database will break compatibility with other databases and is generally not a good idea.

Problem

What's the best way of going about this. Is there something I can put in the migrations? Thanks in advance.

Original source