1

In this migration I have added the statement at the bottom to start user indexes at 1000 in my Postgres DB:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email
      t.string :password_digest

      t.timestamps
    end
    execute "SELECT setval('users_id_seq', 1000);" # <---- This right here
  end
end

Nothing seems to happen. (I have run rake db:reset more than once, nothing seems to work).

This DOES work from the DB console when executed through the command line though. What am I doing wrong?

drewwyatt
  • 5,989
  • 15
  • 60
  • 106
  • how do you know its not running? – sevenseacat May 02 '14 at 04:26
  • @sevenseacat I suppose I did word this weird. I know that it is not *working* because when I create a user the index is still 1 (instead of 1001). When I run the command from the DBConsole, however, the next user created has an ID of 1001. – drewwyatt May 02 '14 at 04:28
  • I think you run this migration without `execute ...` and then add `execute ...` code. But your migration has already run and it won't run twice. If it is last migration try this steps: 1. comment `execute ...` (because it is not reversible) 2. run `rake db:rollback` (**be carefull - this drops your table `users`!**). 3. uncomment `execute ...` and run `rake db:migrate`. PS it is only my assumptions – gotva May 02 '14 at 05:58
  • @gotva I have been running `rake db:reset` to test this. I'll give that a shot. – drewwyatt May 02 '14 at 06:05
  • 2
    according to [this](http://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload) `db:reset` = `db:drop` + `db:setup`. `db:setup` = `db:create` + `db:schema:load` + `db:seed`. So it does NOT run `db:migrate`! – gotva May 02 '14 at 06:07
  • if you do not care about you current data (you use reset which drops DB). Try `rake db:drop db:migrate` and check the result – gotva May 02 '14 at 06:09
  • "ALTER SEQUENCE users_id_seq RESTART WITH 1000" – Ruby Racer May 02 '14 at 06:23

0 Answers0