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 - Rails

Date:February 23, 2016

Recommended Posts

Contact

604-309-8713
info@solidfoundationwebdev.com

Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected