0

So I recently deployed my Rails backend server to Heroku. After a little struggling with passing from SQLite to PostgreSQL, I finally got everything running. Now the problem is, I did rake db:create and rake db:migrate and that went without any errors but now when I make a REST call to the server I have this error (complete) :

2019-07-31T19:42:16.534355+00:00 app[web.1]: I, [2019-07-31T19:42:16.534259 #4]  INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Started POST "/sessions" for 91.176.204.35 at 2019-07-31 19:42:16 +0000
2019-07-31T19:42:16.535824+00:00 app[web.1]: I, [2019-07-31T19:42:16.535063 #4]  INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Processing by SessionsController#create as */*
2019-07-31T19:42:16.535947+00:00 app[web.1]: I, [2019-07-31T19:42:16.535882 #4]  INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5]   Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]"}
2019-07-31T19:42:16.566455+00:00 app[web.1]: I, [2019-07-31T19:42:16.566340 #4]  INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Completed 500 Internal Server Error in 30ms (ActiveRecord: 11.2ms)
2019-07-31T19:42:16.567845+00:00 app[web.1]: F, [2019-07-31T19:42:16.567772 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5]   
2019-07-31T19:42:16.567954+00:00 app[web.1]: F, [2019-07-31T19:42:16.567879 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "users" does not exist
2019-07-31T19:42:16.567957+00:00 app[web.1]: LINE 8:                WHERE a.attrelid = '"users"'::regclass
2019-07-31T19:42:16.567959+00:00 app[web.1]:                                           ^
2019-07-31T19:42:16.567961+00:00 app[web.1]: :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2019-07-31T19:42:16.567962+00:00 app[web.1]:                      pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
2019-07-31T19:42:16.567964+00:00 app[web.1]:                      c.collname, col_description(a.attrelid, a.attnum) AS comment
2019-07-31T19:42:16.567965+00:00 app[web.1]:                 FROM pg_attribute a
2019-07-31T19:42:16.567966+00:00 app[web.1]:                 LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2019-07-31T19:42:16.567968+00:00 app[web.1]:                 LEFT JOIN pg_type t ON a.atttypid = t.oid
2019-07-31T19:42:16.567969+00:00 app[web.1]:                 LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
2019-07-31T19:42:16.567971+00:00 app[web.1]:                WHERE a.attrelid = '"users"'::regclass
2019-07-31T19:42:16.567972+00:00 app[web.1]:                  AND a.attnum > 0 AND NOT a.attisdropped
2019-07-31T19:42:16.567973+00:00 app[web.1]:                ORDER BY a.attnum
2019-07-31T19:42:16.567975+00:00 app[web.1]: ):
2019-07-31T19:42:16.568064+00:00 app[web.1]: F, [2019-07-31T19:42:16.568002 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5]   
2019-07-31T19:42:16.568171+00:00 app[web.1]: F, [2019-07-31T19:42:16.568104 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] app/controllers/sessions_controller.rb:5:in `create'

I have a PostgreSQL add-on installed and it is running. (Otherwise the Rails server wouldn't even boot).

So why do I get this kind of errors on each request? I checked in console and if I do something like User.all I get this :

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "users" does not exist)
LINE 1: SELECT  "users".* FROM "users" LIMIT $1
                               ^
: SELECT  "users".* FROM "users" LIMIT $1

This gets me so confused, what did I do wrong?

1 Answer 1

2

It sounds like you may have run rake db:migrate locally and not on Heroku. As a result, when you use the Heroku CLI, it doesn't recognize the users table because it doesn't yet exist.

I believe if you use have the pg gem in your Gemfile, Heroku's system will automatically setup a database for you. It's been a while since I've created a Heroku instance so don't quote me on that.

If you're unsure if you have PostgreSQL with your app, check the app's service add-ons area. Heroku also has more documentation on using PostgreSQL on their platform. It may be a good idea for you to reference their documentation for getting started with Rails as well.

Once you have your PostgreSQL database setup to work with your app, you use the Heroku CLI to migrate the database and create your users table:

heroku rake db:migrate
Sign up to request clarification or add additional context in comments.

2 Comments

I thought that it was directly transferred, really thanks for that
Ya you have to provision postgres when creating the app in the resources section. Then you have to manually migrate each time you deploy. One way around that is to create a Procfile with a release script that is run each time you deploy. This can include the command to rake db:migrate on each deploy if needed. Also it would be heroku run rake db:migrate if running from the command line. You need to include the run command to access the toolbelt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.