0

I am making an invoice app for my company and want to start my invoices where my old system left( 909). How can I go about this? I thought that modifying the invoices table' primary key to start auto increment at 909. How would you guys go about this? I am using rails 4 and sqlite for db.

Thank you for your time!

davefogo
  • 93
  • 1
  • 9
  • possible duplicate of [Set start value for AUTOINCREMENT in SQLite](http://stackoverflow.com/questions/692856/set-start-value-for-autoincrement-in-sqlite) – infused Apr 02 '15 at 19:03
  • The answer suggested is not explicit on how to do this. Please help out a rookie. Iv'e searched throughly and cannot find an adequate answer. – davefogo Apr 02 '15 at 19:30

1 Answers1

0

Assuming your invoices table is called invoices, run the following SQL query:

UPDATE SQLITE_SEQUENCE SET seq = 909 WHERE name = 'invoices'

You can set the initial value in a migration right after creating the table. In your migration use:

ActiveRecord::Base.connection.execute("UPDATE SQLITE_SEQUENCE SET seq = 909 WHERE name = 'invoices'")
infused
  • 24,000
  • 13
  • 68
  • 78
  • Infused thanks for your answer. Could you please walk me through? Is this to be done through rails console? ActiveRecord::Base.connection.execute? Thanks – davefogo Apr 02 '15 at 20:45
  • Yes, you can either run it via `ActiveRecord::Base.connection.execute` or directly through the `sqlite3` console. – infused Apr 02 '15 at 21:00
  • So just run sqlite3 db/development.sqlite3 to enter Sql console right? I lve had trouble through irb. – davefogo Apr 02 '15 at 21:03
  • Yes, that's correct. Then you should just be able to paste in the SQL followed by a semi-colon. – infused Apr 02 '15 at 21:04