Manual assignment of ID object



  • I want to create an object and hand it to ID. For example Model.create(id: 100, title: 'Object 100')

    How do we do when the next object is Model.create?



  • https://stackoverflow.com/questions/2075331/change-starting-id-number that we need to set up an auto-increment counter for the table field. I mean, the auto increment is not controlled by Rails, it's the SBS.

    For MySQL, you have to fulfil:

    ActiveRecord::Base.connection.execute("ALTER TABLE table_name AUTO_INCREMENT = 100")
    

    For Postgres:

    ActiveRecord::Base.connection.execute("ALTER SEQUENCE table_name_id_seq START with 100 RESTART;")
    

    We need to generate the migration, put the code in there, replace it. table_name in the name of the table, launch the migration.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2