Rails

Reset An Auto Increment Field In Model On Rails With SQL

Posted by Weston Ganger on February 23, 2016

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:


Posted in Rails and Tagged with sql rails 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts