Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG::UndefinedTable: ERROR: relation "..." does not exist

I know this has been asked so many times but I have tried many solutions with no luck. I have a Rails app deployed on Heroku, it works locally but I get "We're sorry, but something went wrong." when I try to launch it on Heroku.

I have tried

heroku run rake db:migrate
heroku restart
heroku open 

but the table already exists in schema

I have tried:

rake db:reset

and rake db:drop db:create db:migrate

I know that by default, changes are done in the development environment, could this be the reason? I'm running the changes in the development env but the 'heroku open' opens the prod env?

Update, the complete snippet for heroku logs: heroku logs

    2015-12-12T05:47:57.814680+00:00 app[web.1]:    /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:32:in `start'
2015-12-12T05:47:57.814679+00:00 app[web.1]:    /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:170:in `block in start'
2015-12-12T05:47:57.814682+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:264:in `start'
2015-12-12T05:47:57.814680+00:00 app[web.1]:    /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:160:in `start'
2015-12-12T05:47:57.814683+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.6/lib/rails/commands/server.rb:69:in `start'
2015-12-12T05:47:57.814681+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/handler/webrick.rb:14:in `run'
2015-12-12T05:47:57.814684+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:81:in `block in server'
2015-12-12T05:47:57.814685+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:76:in `tap'
2015-12-12T05:47:57.814686+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:76:in `server'
2015-12-12T05:47:57.814686+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
2015-12-12T05:47:57.814687+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.6/lib/rails/commands.rb:17:in `<top (required)>'
2015-12-12T05:47:57.814688+00:00 app[web.1]:    bin/rails:8:in `require'
2015-12-12T05:47:57.814688+00:00 app[web.1]:    bin/rails:8:in `<main>'
2015-12-12T05:47:57.814711+00:00 app[web.1]: [2015-12-12 05:47:57] INFO  going to shutdown ...
2015-12-12T05:47:57.814800+00:00 app[web.1]: Exiting
2015-12-12T05:47:57.814767+00:00 app[web.1]: [2015-12-12 05:47:57] INFO  WEBrick::HTTPServer#start done.
2015-12-12T05:47:58.668656+00:00 heroku[web.1]: Process exited with status 143
2015-12-12T15:25:42.166048+00:00 heroku[web.1]: Unidling
2015-12-12T15:25:42.166363+00:00 heroku[web.1]: State changed from down to starting
2015-12-12T15:25:43.961413+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 31053 -e production`
2015-12-12T15:25:47.675210+00:00 app[web.1]: [2015-12-12 15:25:47] INFO  WEBrick 1.3.1
2015-12-12T15:25:47.675235+00:00 app[web.1]: [2015-12-12 15:25:47] INFO  ruby 2.0.0 (2014-11-13) [x86_64-linux]
2015-12-12T15:25:47.675544+00:00 app[web.1]: [2015-12-12 15:25:47] INFO  WEBrick::HTTPServer#start: pid=3 port=31053
2015-12-12T15:25:48.347999+00:00 heroku[web.1]: State changed from starting to up
2015-12-12T15:25:49.238228+00:00 app[web.1]: => Booting WEBrick
2015-12-12T15:25:49.238232+00:00 app[web.1]: => Rails 4.1.6 application starting in production on http://0.0.0.0:31053
2015-12-12T15:25:49.238233+00:00 app[web.1]: => Run `rails server -h` for more startup options
2015-12-12T15:25:49.238235+00:00 app[web.1]: => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
2015-12-12T15:25:49.238235+00:00 app[web.1]: => Ctrl-C to shutdown server
2015-12-12T15:25:49.238236+00:00 app[web.1]: Started GET "/" for 98.196.181.134 at 2015-12-12 15:25:49 +0000
2015-12-12T15:25:49.371270+00:00 app[web.1]: Processing by UsersController#index as HTML
2015-12-12T15:25:49.385278+00:00 heroku[router]: at=info method=GET path="/" host=appname.herokuapp.com request_id=276e00fc-cadd-4b08-ac52-6d72099c9c2e fwd="98.196.181.134" dyno=web.1 connect=1ms service=156ms status=500 bytes=1754
2015-12-12T15:25:49.384220+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users"
2015-12-12T15:25:49.384216+00:00 app[web.1]: PG::UndefinedTable: ERROR:  relation "users" does not exist
2015-12-12T15:25:49.384221+00:00 app[web.1]:                                   ^
2015-12-12T15:25:49.384222+00:00 app[web.1]: : SELECT "users".* FROM "users"
2015-12-12T15:25:49.384742+00:00 app[web.1]:   Rendered users/index.html.erb within layouts/application (3.7ms)
2015-12-12T15:25:49.384868+00:00 app[web.1]: Completed 500 Internal Server Error in 14ms
2015-12-12T15:25:49.387101+00:00 app[web.1]: 
2015-12-12T15:25:49.387105+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users"
2015-12-12T15:25:49.387103+00:00 app[web.1]: ActionView::Template::Error (PG::UndefinedTable: ERROR:  relation "users" does not exist
2015-12-12T15:25:49.387105+00:00 app[web.1]:                                   ^
2015-12-12T15:25:49.387106+00:00 app[web.1]: : SELECT "users".* FROM "users"):
2015-12-12T15:25:49.387107+00:00 app[web.1]:     15: 
2015-12-12T15:25:49.387107+00:00 app[web.1]:     14:   </thead>
2015-12-12T15:25:49.387110+00:00 app[web.1]:     17:     <% @users.each do |user| %>
2015-12-12T15:25:49.387110+00:00 app[web.1]:     18:       <tr>
2015-12-12T15:25:49.387108+00:00 app[web.1]:     16:   <tbody>
2015-12-12T15:25:49.387112+00:00 app[web.1]:     20:         <td><%= user.address %></td>
2015-12-12T15:25:49.387111+00:00 app[web.1]:     19:         <td><%= user.name %></td>
2015-12-12T15:25:49.387116+00:00 app[web.1]:   app/views/users/index.html.erb:17:in `_app_views_users_index_html_erb___3405507757113956440_70276178207640'
2015-12-12T15:25:49.387118+00:00 app[web.1]: 
2015-12-12T15:25:49.387117+00:00 app[web.1]: 
2015-12-12T15:27:59.501112+00:00 heroku[router]: at=info method=GET path="/" host=appname.herokuapp.com request_id=279ab493-5a01-4b89-b0a8-cbc9cb51fe83 fwd="98.196.181.134" dyno=web.1 connect=0ms service=18ms status=500 bytes=1754
2015-12-12T15:27:59.486906+00:00 app[web.1]: Started GET "/" for 98.196.181.134 at 2015-12-12 15:27:59 +0000
2015-12-12T15:27:59.496321+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users"
2015-12-12T15:27:59.496322+00:00 app[web.1]:                                   ^
2015-12-12T15:27:59.496318+00:00 app[web.1]: PG::UndefinedTable: ERROR:  relation "users" does not exist
2015-12-12T15:27:59.496323+00:00 app[web.1]: : SELECT "users".* FROM "users"
2015-12-12T15:27:59.499226+00:00 app[web.1]: 
2015-12-12T15:27:59.496875+00:00 app[web.1]:   Rendered users/index.html.erb within layouts/application (2.7ms)
2015-12-12T15:27:59.496993+00:00 app[web.1]: Completed 500 Internal Server Error in 4ms
2015-12-12T15:27:59.499229+00:00 app[web.1]: ActionView::Template::Error (PG::UndefinedTable: ERROR:  relation "users" does not exist
2015-12-12T15:27:59.499231+00:00 app[web.1]:                                   ^
2015-12-12T15:27:59.499230+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users"
2015-12-12T15:27:59.499231+00:00 app[web.1]: : SELECT "users".* FROM "users"):
2015-12-12T15:27:59.499233+00:00 app[web.1]:     16:   <tbody>
2015-12-12T15:27:59.499232+00:00 app[web.1]:     14:   </thead>
2015-12-12T15:27:59.499233+00:00 app[web.1]:     15: 
2015-12-12T15:27:59.499234+00:00 app[web.1]:     17:     <% @users.each do |user| %>
2015-12-12T15:27:59.499235+00:00 app[web.1]:     18:       <tr>
2015-12-12T15:27:59.499239+00:00 app[web.1]: 
2015-12-12T15:27:59.499237+00:00 app[web.1]:     20:         <td><%= user.address %></td>
2015-12-12T15:27:59.499236+00:00 app[web.1]:     19:         <td><%= user.name %></td>
2015-12-12T15:27:59.499239+00:00 app[web.1]:   app/views/users/index.html.erb:17:in `_app_views_users_index_html_erb___3405507757113956440_70276178207640'
2015-12-12T15:27:59.499240+00:00 app[web.1]: 
2015-12-12T15:27:59.493196+00:00 app[web.1]: Processing by UsersController#index as HTML
2015-12-12T15:28:02.359919+00:00 app[web.1]: Started GET "/" for 98.196.181.134 at 2015-12-12 15:28:02 +0000
2015-12-12T15:28:02.366634+00:00 app[web.1]: PG::UndefinedTable: ERROR:  relation "users" does not exist
2015-12-12T15:28:02.366638+00:00 app[web.1]:                                   ^
2015-12-12T15:28:02.366639+00:00 app[web.1]: : SELECT "users".* FROM "users"
2015-12-12T15:28:02.366637+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users"
2015-12-12T15:28:02.367435+00:00 app[web.1]:   Rendered users/index.html.erb within layouts/application (3.1ms)
2015-12-12T15:28:02.367587+00:00 app[web.1]: Completed 500 Internal Server Error in 4ms
2015-12-12T15:28:02.369955+00:00 app[web.1]: 
2015-12-12T15:28:02.369959+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users"
2015-12-12T15:28:02.370984+00:00 heroku[router]: at=info method=GET path="/" host=appname.herokuapp.com request_id=2b1fbddf-0ef7-4d8c-9107-9e67a2a1ca6a fwd="98.196.181.134" dyno=web.1 connect=0ms service=14ms status=500 bytes=1754
2015-12-12T15:28:02.369958+00:00 app[web.1]: ActionView::Template::Error (PG::UndefinedTable: ERROR:  relation "users" does not exist
2015-12-12T15:28:02.363203+00:00 app[web.1]: Processing by UsersController#index as HTML
2015-12-12T15:28:02.369960+00:00 app[web.1]:                                   ^
2015-12-12T15:28:02.369960+00:00 app[web.1]: : SELECT "users".* FROM "users"):
2015-12-12T15:28:02.369961+00:00 app[web.1]:     14:   </thead>
2015-12-12T15:28:02.369962+00:00 app[web.1]:     15: 
2015-12-12T15:28:02.369963+00:00 app[web.1]:     17:     <% @users.each do |user| %>
2015-12-12T15:28:02.369964+00:00 app[web.1]:     18:       <tr>
2015-12-12T15:28:02.369962+00:00 app[web.1]:     16:   <tbody>
2015-12-12T15:28:02.369964+00:00 app[web.1]:     19:         <td><%= user.name %></td>
2015-12-12T15:28:02.369965+00:00 app[web.1]:     20:         <td><%= user.address %></td>
2015-12-12T15:28:02.369966+00:00 app[web.1]:   app/views/users/index.html.erb:17:in `_app_views_users_index_html_erb___3405507757113956440_70276178207640'
2015-12-12T15:28:02.369967+00:00 app[web.1]: 
2015-12-12T15:28:02.369967+00:00 app[web.1]:

Update: Following on @Vincent's answer, I can tell that the table is not being created on Heroku upon migration, so I tried loading the db from schema.rb since my schema has the table,

$ rake db:schema:load RAILS_ENV=production --trace
** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:schema:load
-- enable_extension("plpgsql")
   -> 0.1872s
-- create_table("users", {:force=>true})
   -> 0.1565s
-- initialize_schema_migrations_table()
   -> 0.0876s
$ heroku restart
Restarting dynos... done
$ heroku open
Opening appname... done
$ heroku pg:psql
---> Connecting to HEROKU_POSTGRESQL_WHITE_URL (DATABASE_URL)
psql (9.4.5, server 9.3.9)

appname::WHITE=> \d
                  List of relations
 Schema |       Name        | Type  |     Owner      
--------+-------------------+-------+----------------
 public | schema_migrations | table | ownername
(1 row)

$ heroku run rails c
Running rails c on appname... up, run.1689
Loading production environment (Rails 4.1.6)
irb(main):001:0> ActiveRecord::Base.connection.tables
=> ["schema_migrations"]
irb(main):002:0> 

As you can see, the table users wasn't created. Also, schema_migrations is empty.

What am I missing? Why is not the table users being created in the prod env?

like image 648
user1210182 Avatar asked Dec 12 '15 05:12

user1210182


1 Answers

It looks like your migrations that you ran on Heroku didn't actually create the table. You can check by running heroku run rails c and try ActiveRecord::Base.connection.tables to list your tables

Sometimes PG will have different effects to migration compared to Sqlite3/MySQL. It is recommended to run PG on local dev to mirror the migration and solve the problem before deployment

like image 195
Vincent Avatar answered Oct 13 '22 18:10

Vincent