rspec returns “PG::Error: ERROR: relation ”table_name“ does not exist”

杀马特。学长 韩版系。学妹 提交于 2019-12-03 03:34:06

I've run into this in two instances (updated 6/13/2012):

First:

I haven't yet migrated my test database...

rake db:migrate

...which you need to do before both db:test:prepare and db:test:load.

When you invoke rspec it should prepare and load your database for you anyway. You shouldn't have to do that by hand.

Second:

A typo in my migration. I accidentally reversed my table and column names in the parameter list.

A migration on a Rails 3.1 project:

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

...which is wrong, because has_import_errors and import_data_cache are my column names, and they therefore should come second, not first.

The correct migration, with the table name first was:

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

This usually happens when you have rspec running while migrating (usually via guard). A simple solution is to quit guard, do the migration and restart guard.

It typically indicates that there is another open PostgreSql connection. To bubble up the right error try % rake db:test:prepare

Running test prepare showed the following below and when the open connection (PGAdmin in my case) was closed the issue was resolved.

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)

Just hit this same error, nothing worked but dropping and re-creating the database, this fixed it for me.

rake db:drop db:create db:migrate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!