How to reset auto increment field in a ActiveRecord migration?

后端 未结 3 2007
囚心锁ツ
囚心锁ツ 2021-02-07 11:46

In my migration I have:

def up
   MyModel.destroy_all
   MyModel.create!({:id=>1,:name=>\'foo\'})
   MyModel.create!({:id=>2,:name=>\'fooBar\'})
   M         


        
3条回答
  •  终归单人心
    2021-02-07 12:10

    In case anyone else wonders how to do this with MYSQL: here is the code one would use in the migration in order to reset the auto increment for a table:

    ActiveRecord::Base.connection.execute('ALTER TABLE foo_bars AUTO_INCREMENT = 1')
    
    • Where foo_bars would be the table name whose auto_increment you are resetting.

    I figured this out thanks to the answer for this question.

提交回复
热议问题