使用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视图
发布评论