rspec无法识别Postgres视图

编程入门 行业动态 更新时间:2024-10-23 15:20:59
本文介绍了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是属性表上的视图)

(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视图

本文发布于:2023-10-16 16:19:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   无法识别   rspec   Postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!