When I am working on a data import of some kind, I sometimes need to reset the AUTO_INCREMENT value for the id column.

The SQL for this is ALTER TABLE table_name AUTO_INCREMENT = 1;

Just run this in your rails console replacing ‘posts’ with your table name.

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

You can change the value of 1 to something higher if you need it to start at maybe 100 for example.

Related External Links: