ruby-on-rails - PG::UndefinedTable: 错误:关系 "..."不存在

标签 ruby-on-rails ruby heroku

我知道这个问题已经被问过很多次了,但我已经尝试了很多解决方案,但都没有成功。我在 Heroku 上部署了一个 Rails 应用程序,它可以在本地运行,但我收到“很抱歉,出了点问题”。当我尝试在 Heroku 上启动它时。

我试过了

heroku run rake db:migrate
heroku restart
heroku open 

但是表已经存在于schema中

我试过:

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> 

如您所见,未创建表 users。此外,schema_migrations 为空。

我错过了什么?为什么不在产品环境中创建表用户?

最佳答案

看起来您在 Heroku 上运行的迁移实际上并未创建该表。您可以通过运行 heroku run rails c 检查并尝试 ActiveRecord::Base.connection.tables 列出您的表

与Sqlite3/MySQL相比,PG有时会对迁移产生不同的影响。建议在本地dev上运行PG镜像迁移,部署前解决问题

关于ruby-on-rails - PG::UndefinedTable: 错误:关系 "..."不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236641/

相关文章:

ruby - 在 Ruby/Rails 中,如何对 URL 中的特殊字符进行编码/转义?

mysql - 从 v4.0.0 开始需要显式提供方言/建议的修复不起作用

ruby-on-rails - Rspec 和 'before_validation' 回调问题

ruby-on-rails - 外部测试函数(Rails)

ruby - 从 ruby​​ 中的字符串中删除空行

ruby-on-rails - 有没有办法在没有 mod_rewrite 的情况下使用 Ruby on Rails 将 www.foo.com/* 重定向到 foo.com/*

ruby-on-rails - SSL 证书 WPEngine 到 Heroku Rails

ruby-on-rails - Active Storage 永久镜像 URL

ruby-on-rails - 为什么我的 Capybara/Poltergeist 测试无法从 jQuery 自动完成字段中进行选择?

ruby - 使用 RVM 在 OSX 10.15.2 Catalina 中安装 Ruby 2.3.8 时出错;未找到 OpenSSL