Rohan Panchal
Rohan Panchal

Reputation: 1266

ActiveRecord AutoIncrement Value

I am using active record with mysql on a rails web app.

After saving some active record models i noticed that the id field is being incremented by 10. Shouldn't it e by 1?

If i can change it what is the rake or activerecord migration command

Upvotes: 1

Views: 1342

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 3427

in rails console type

1. for postgresql

ActiveRecord::Base.connection.execute("ALTER SEQUENCE table_name_id_seq RESTART WITH 1")

2. for mysql

ActiveRecord::Base.connection.execute("ALTER TABLE table_name AUTO_INCREMENT=1")

where table_name is your actual table name

using migration

class SetAutoIncrement < ActiveRecord::Migration
  def self.up
    execute "ALTER TABLE table_name AUTO_INCREMENT=1"
  end

  def self.down
  end
end

Upvotes: 6

Related Questions