Brett Sanders
Brett Sanders

Reputation: 813

PGError: ERROR: relation "users" does not exist - Rails Tutorial 2nd Edition Chapter 9 Heroku

At end of Chapter 9 of Hartl's Rails Tutorial (2nd Edition) ran into an error when populating the db using a rake task. Ended up resolving it, but not sure what went wrong. In case anyone else hits this error, here's what I did. If anyone knows what went wrong, please comment --I'd love to know. Thanks!

Ran these commands

$ git push heroku
$ heroku run rake db:migrate
$ heroku pg:reset SHARED_DATABASE --confirm <name-heroku-gave-to-your-app>
$ heroku run rake db:populate

Then, got this error:

rake aborted!
PGError: ERROR:  relation "users" does not exist
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"users"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Tasks: TOP => db:populate
(See full trace by running task with --trace)

Couldn't figure out what was wrong. Then came across this post: Heroku Postgres Error: PGError: ERROR: relation "organizations" does not exist (ActiveRecord::StatementInvalid)

From that, I just ran these commands:

heroku run rake db:reset
heroku run rake db:migrate
heroku run rake db:populate

Now the app is working. Still not clear to me why I got that error when resetting SHARED_DATABASE ... If anyone knows, curious to find out.

Upvotes: 3

Views: 6807

Answers (3)

Chris Hart
Chris Hart

Reputation: 2153

In case someone else comes across this, there is a different problem that has the same symptom. If you have an unmigrated table that also has an AR observer that is set in application.rb, rake will not load because it tries to load the underlying model that is being observed. You have to migrate, then add the AR observer to application.rb.

Upvotes: 2

mgsakata
mgsakata

Reputation: 99

I had the exact same problem but had executed the commands in the correct order. Had to run the following after all the rake commands and it then worked fine.

$ heroku restart

Upvotes: 6

Jason Noble
Jason Noble

Reputation: 3766

You have the steps out of order. They should be:

$ git push heroku
$ heroku pg:reset SHARED_DATABASE --confirm <name-heroku-gave-to-your-app>
$ heroku run rake db:migrate
$ heroku run rake db:populate

Hat Tip to @rjz

Upvotes: 7

Related Questions