【问题标题】:rspec returns "PG::Error: ERROR: relation "table_name" does not exist"rspec 返回“PG::Error: ERROR: 关系“table_name”不存在”
【发布时间】:2012-03-05 05:47:00
【问题描述】:

环境是 rvm、rspec 2.8.0、rails 3.0.6 和 pg 0.13.2 上的 REE(2011.12)。在 CentOS 5.6 上使用 PostgreSQL 8.3.17。 db:migrate 正常工作。但是 rspec 有以下错误。

1) ApiController articles OK
 Failure/Error: Unable to find matching line from backtrace
 ActiveRecord::StatementInvalid:
   PG::Error: ERROR:  relation "table_name" does not exist
   : DELETE FROM "table_name"

我正在将我的项目从带有 rspec 1.x 系列的 rails 2.3.5 更新到带有 rspec2 的 rails 3.0。复制了所有的 rspec 测试,我已经合并了旧的 spec_helper.rb 和新的(它是生成的rails g rspec:install)。

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true

end

我阅读了有关此错误的类似问题。所以我尝试了rake db:test:preparerake db:test:load,但没有解决。你有什么主意吗? 看起来测试没有在测试数据库上运行......我该怎么办? :(

【问题讨论】:

    标签: ruby-on-rails-3 postgresql rspec rspec2 rails-postgresql


    【解决方案1】:

    我在两个实例中遇到过这种情况(2012 年 6 月 13 日更新):

    第一:

    我还没有迁移我的测试数据库...

    rake db:migrate

    ...您需要在db:test:preparedb:test:load 之前执行此操作。

    当您调用 rspec 时,无论如何它都应该为您提供 prepareload 您的数据库。您不应该手动执行此操作。

    第二:

    我的迁移中的一个错字。我不小心颠倒了参数列表中的表名和列名。

    Rails 3.1 项目的迁移:

    def change
      add_column :had_import_errors, :studies, :boolean, :default => false
      add_column :import_data_cache, :studies, :text
    end
    

    ...这是错误,因为has_import_errorsimport_data_cache 是我的列名,因此它们应该排在第二位,而不是排在第一位。

    正确的迁移,首先是表名:

    def change
      add_column :studies, :had_import_errors, :boolean, :default => false
      add_column :studies, :import_data_cache, :text
    end
    

    【讨论】:

    • 我显然忘记了rake db:test:load。谢谢。
    【解决方案2】:

    这通常发生在您在迁移时运行 rspec(通常通过保护)。一个简单的解决方案是退出警卫,执行迁移并重新启动警卫。

    【讨论】:

      【解决方案3】:

      它通常表示有另一个打开的 PostgreSql 连接。要找出正确的错误,请尝试% rake db:test:prepare

      运行测试准备显示以下内容,当打开的连接(在我的情况下为 PGAdmin)关闭时,问题得到解决。

      rake aborted!
      PG::Error: ERROR:  database "xyz_test" is being accessed by other users
      DETAIL:  There are 1 other session(s) using the database.
      : DROP DATABASE IF EXISTS "xyz_test"
      
      Tasks: TOP => db:test:load => db:test:purge
      (See full trace by running task with --trace)
      

      【讨论】:

        【解决方案4】:

        刚刚遇到同样的错误,除了删除并重新创建数据库之外没有任何效果,这为我修复了它。

        rake db:drop db:create db:migrate

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-14
          • 2012-07-16
          • 2019-05-14
          • 2022-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-14
          相关资源
          最近更新 更多