PG::UndefinedTable: 错误:关系 "... " 不存在。

7

我知道这个问题已经被问了很多次,但我试过许多解决方案都没有成功。我有一个部署在Heroku上的Rails应用程序,它在本地工作正常,但当我尝试在Heroku上启动它时,会显示"我们很抱歉,出了点问题。"。

我已经尝试过:

heroku run rake db:migrate
heroku restart
heroku open 

但是该表已经存在于模式中。

我尝试过:

rake db:reset

并且 rake db:drop db:create db:migrate

我知道默认情况下更改是在开发环境中完成的,这可能是原因吗?我正在开发环境中运行更改,但 'heroku open' 打开了生产环境?

更新,heroku日志的完整片段: heroku日志

    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]:

更新:继@Vincent的回答之后,我可以确认在迁移时表格没有被创建在Heroku上,所以我尝试从schema.rb加载数据库,因为我的架构中有这个表。
$ 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> 

您可以看到,用户表并未被创建。此外,模式迁移为空。

我错过了什么?为什么生产环境中没有创建用户表?


你需要从你的Heroku日志中添加更多的代码片段。 - x6iae
尝试在应用程序中设置一个不使用数据库连接的端点,看看它是否正常工作。如果它能正常工作,那么问题可能出现在生产环境的迁移上。您可以通过暂时打开生产环境中的错误显示(https://dev59.com/4Ggu5IYBdhLWcg3wEzHP)来确保这一点。 - Ashutosh Agrawal
@SunnyK 已完成。 @AshutoshAgrawal 我更改了文件,结果出现 ActiveRecord::StatementInvalid in Users#index在 /app/app/views/users/index.html.erb 中第17行引发错误:PG::UndefinedTable: ERROR: relation "users" does not exist LINE 1: SELECT "users".* FROM "users" ^ : SELECT "users".* FROM "users"这与此 (http://stackoverflow.com/questions/18480283/activerecordstatementinvalid-in-pinsindex-showing-app-app-views-pins-index-h) 非常相似,但解决方案对我不起作用,我尝试了迁移数据库然后推送和往返操作! - user1210182
你解决了你的问题吗? - DoruChidean
不幸的是,我最终放弃了/删除了所有内容并重新开始,不确定为什么一开始它就无法正常工作。 - user1210182
2个回答

8
似乎你在Heroku上运行的迁移并没有真正创建表。你可以通过运行 heroku run rails c 并尝试使用 ActiveRecord :: Base.connection.tables 列出您的表来进行检查。
有时,PG在迁移方面与Sqlite3 / MySQL产生不同的影响。建议在本地开发中运行PG以镜像迁移并在部署之前解决问题。

感谢反馈。你是对的,表还没有被创建。我尝试过:heroku pg:psql 然后 \d 列出可用的数据库,我得到了一个表,那就是 schema_migrations,确实在运行 select * from schema_migrations; 后,它返回零行。我知道如何通过打开服务器然后访问 localhost:3000 在本地运行应用程序,怎么可以在本地开发中运行 PG? - user1210182
查看页面以设置PG。如果您使用的是Ubuntu:[链接](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04)。在计算机上安装PG需要更多的操作。因此,请遵循指南以帮助您进行设置。 - Vincent

2

ActionView::Template::Error (PG::UndefinedTable: ERROR: relation "users" does not exist

这可能是由于在Heroku上部署Rails应用程序时缺少数据库导致的。

对于Rails > 6: heroku run rails db:migrate


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接