Archive for February 2009
MySQL: Reset ID Auto-Increment
ALTER TABLE tablename AUTO_INCREMENT = 1
Auto-increment is the thing that makes the primary key (integer) increment by 1 each time a record is inserted. Say you have 10 records (1..10). If you delete 9 and 10, the next ID is going to be 11, not 9. However, if you run the SQL command above, you can reset it to the last record ID, plus 1 – making the next record in this example, 9.
If you happen to want to do this in a migration file, just use “execute.”
def self.up
execute 'ALTER TABLE tablename AUTO_INCREMENT = 1'
# then go on to populate the right way
Skill.create(:id => 1, :name => 'Ajax')
...
end
def self.down
Skill.find(:all).each { |s| s.destroy }
end