Change starting id number

I have an 'Account' model in Rails with its corresponding 'accounts' table in the database. If I wipe the database and start over, the 'account_id' field will always start at 1 and count up from there. I would like to change the starting number, so that, when the very first account is created in a fresh database, the 'account_id' is, say, 1000. Is there a way to do that in Rails, or do I need specialized database-dependent SQL code?

For the sake of illustration, here is a simplified version of my 'accounts' table:

create_table "accounts", :force => true do |t|
  t.string   "email", :null => false
  t.string   "crypted_password", :null => false
  t.string   "name", :null => false
  t.boolean  "email_verified", :default => false
end

Answers


You'll need to do some specialized database-dependent SQL to get this functionality.

If you're using MySQL, you can add the following code to your migration after the create_table code:

execute("ALTER TABLE tbl AUTO_INCREMENT = 1000")

for postgres:

execute("ALTER SEQUENCE accounts_id_seq START with 1000 RESTART;")

see http://www.postgresql.org/docs/8.4/static/sql-altersequence.html


Need Your Help

mySQL order by date field not working

mysql sql-order-by

I am trying to get a data sorted from 2 tables posts and postmeta

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.