rspec 无法识别 Postgres 视图 [英] Postgres views not recognized by rspec

查看:18
本文介绍了rspec 无法识别 Postgres 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Rails 3.2.18 和 Postgres,我有几个表,我为它们创建了一个视图以最小化 ActiveRecord 加载的数据.在应用程序中,一切似乎都在正确运行,但是当我尝试运行 Rspec 时,我得到:

Using Rails 3.2.18 and Postgres, I have a few tables for which I have created a view to minimize the data loaded by ActiveRecord. Everything appears to be running correctly when in the application, but when I try to run Rspec I get:

 ActiveRecord::StatementInvalid:
       PG::UndefinedTable: ERROR:  relation "properties_view" does not exist

(其中properties_view"是properties"表上的视图)

(where 'properties_view' is the view on the 'properties' table)

如何确保 Rspec 从 Postgres 正确加载视图?

What can I do to ensure that Rspec loads views properly from Postgres?

推荐答案

Rails 并不真正理解像视图这样的高级"数据库概念,因此它们不会出现在您的 schema.rb 中.当 rspec 设置它的测试数据库时,它将使用 schema.rb 创建数据库模式,因为你不会在 schema.rb 中找到你的视图,你赢了在 rspec 将使用的测试数据库中找不到您的视图,一切都将分崩离析.

Rails doesn't really understand "advanced" database concepts like views so they won't appear in your schema.rb. When rspec is setting up its test database, it will use schema.rb to create the database schema, since you won't find your views in schema.rb, you won't find your views in the test database that rspec will be using and everything falls apart.

解决方案是从schema.rb 切换到structure.sql.你应该能够更新你的 config/application.rb 说:

The solution is to switch from schema.rb to structure.sql. You should be able to update your config/application.rb to say:

config.active_record.schema_format = :sql

然后执行 rake db:structure:dump 以生成 structure.sql 文件.完成后,从文件系统和修订控制中删除 schema.rb,添加 structure.sql,然后重试.

and then do a rake db:structure:dump to generate the structure.sql file. Once you have that, remove schema.rb from your file system and revision control, add structure.sql, and try again.

这篇关于rspec 无法识别 Postgres 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆