1

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

2
  • Did you deleted some records from the table before? In that case it happens like this.
    – Pavan
    Commented Jun 13, 2014 at 16:59
  • I didn't. I even reset the entire dev database instance. Both times the id fields keep incrementing by 10 Commented Jun 13, 2014 at 17:03

1 Answer 1

6

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
3
  • This works. But how do i enable this by default when i create the models? Commented Jun 13, 2014 at 17:15
  • you can do it in migration also Commented Jun 13, 2014 at 17:19
  • @RohanPanchal see the updated answer,although i have never tried like that so m not sure. Commented Jun 13, 2014 at 17:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.