Reset An Auto Increment Field In Model On Rails With SQL

Posted By Weston Ganger

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:

Article Topic:Software Development - Ruby / Rails

Date:February 23, 2016