【问题标题】:How to work with tests and fixtures in a database with foreign key contraints in Rails?如何在 Rails 中使用具有外键约束的数据库中的测试和夹具?
【发布时间】:2014-09-02 16:25:57
【问题描述】:

我正在使用 Rails 4.1 和 PostgreSQL,并且我正在使用 foreigner gem 创建外键约束。但我对fixtures 有一些问题。

当我跑步时:

spring rake test

我收到如下错误:

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  update or delete on table "pessoas" violates foreign key constraint "pacientes_pessoa_id_fk" on table "pacientes"
DETAIL:  Key (id)=(980190962) is still referenced from table "pacientes".
: DELETE FROM "pessoas"

我找到了解决方法:重新创建数据库:

RAILS_ENV=test spring rake db:reset && spring rake test

当我尝试使用 rake db:fixture:load 使用固定装置为开发数据库播种时出现某种错误。

我已经尝试更改test_helper.rb 中的夹具加载顺序,但这还不够。

有人知道怎么解决吗?我在网上搜索了很多,但没有找到解决方案。

【问题讨论】:

  • 这方面有什么进展吗?
  • 可以同时运行多个rake脚本RAILS_ENV=test spring rake db:reset test

标签: ruby-on-rails foreign-keys fixtures minitest foreigner


【解决方案1】:

所以,我确信这是可行的,但我不能说这是一个真正正确的解决方法。

你需要让用户成为超级用户。

在我使用 Cloud 9 的情况下,我登录的用户是“ubuntu”。

我输入 sudo -u postgres psql (实际上是 sudo 两次让 C9 正确访问)并输入:

更改用户 ubuntu 超级用户;

退出,它会起作用。

如果您运行tail log/test.rb,您会在运行集成测试时看到PG::InsufficientPrivilege: ERROR,因此用户没有足够的权限。你不会想在生产中这样做,但我的生产是在 Heroku 上的,所以我很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多