Rails-PG :: UndefinedTable:错误:关系“客户”不存在

时间:2019-10-11 19:55:25

标签: postgresql ruby-on-rails-5

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

2 个答案:

答案 0 :(得分:0)

我认为您忘记了进行迁移:

bundle exec rake db:migrate

答案 1 :(得分:0)

我终于想通了。我再次通读错误输出,发现以下行:

/home/jdc44/Websites/myapp/spec/factories/job.rb:2:in在'`中阻止。

job.rb
FactoryGirl.define do
  factory :job, :class => Job do
    customer_id FactoryGirl.create(:customer).id 
    ...

由于错误在customers表上,因此我认为这与jobs规范有关,因为工作和客户之间存在关系:customer has many jobs工作规范有这个:

before do
    @job = FactoryGirl.build(:job)
  end

由于某种原因,将build更改为create可使一切正常。不知道为什么,但是它正在工作。