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 

Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts