首页 > 解决方案 > Rails - PG :: UndefinedTable:错误:关系“客户”不存在

问题描述

Rails 5.0.4、Ruby 2.5.1、Postgres、Ubuntu 18.04

我已经在 stackoverflow 中阅读了与我的问题类似的几篇文章,但没有任何内容对我有帮助或与我的问题相似。

背景故事

我在笔记本电脑上开发了一个个人应用程序。我推送到 github 和 heroku,一切正常。我也希望能够在我的台式电脑上使用这个应用程序。它与我的笔记本电脑具有相同的设置:都运行 Ubuntu 18.04,都具有相同版本的 ruby​​ 和 rails,都具有 postgres。

我将应用程序从 github 克隆到我的桌面上。问题:我运行的任何 rails 或 rake 命令都会生成此错误。

/home/jdc44/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:88:in `async_exec': PG::UndefinedTable: ERROR:  relation "customers" does not exist
LINE 8:                WHERE a.attrelid = '"customers"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
             (SELECT c.collname FROM pg_collation c, pg_type t
               WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation),
                     col_description(a.attrelid, a.attnum) AS comment
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"customers"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
 (ActiveRecord::StatementInvalid)

既然它说PG::UndefinedTable: ERROR...,我“假设”它一定是数据库设置问题。

在我的桌面上,在 postgres 中,我创建了与笔记本电脑上相同的数据库、用户和密码。

postgres=# \l
                                           List of databases
          Name          |   Owner    | Encoding |   Collate   |    Ctype    |     Access privileges     
------------------------+------------+----------+-------------+-------------+---------------------------
 myapp                  | myapp      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 myapp_development      | cowan      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/cowan                +
                        |            |          |             |             | cowan=CTc/cowan          +
                        |            |          |             |             | myapp=CTc/cowan
 myapp_test             | myapp      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/myapp                +
                        |            |          |             |             | myapp=CTc/myapp
 postgres               | postgres   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 

除了Access privileges列中显示的内容外,带有 myapp... 的行与我的笔记本电脑输出相同。

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: localhost
  username: myapp
  password: *****

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

同样,任何rakerails命令都会让我遇到这个错误。即:rake db:migraterails console

任何建议将不胜感激。

jc

标签: postgresqlruby-on-rails-5

解决方案


我想你忘了运行你的迁移:

bundle exec rake db:migrate

推荐阅读