4

我正在使用 Rails 4.1 和 PostgreSQL,并且正在使用foreigner gem来创建外键约束。但是我在固定装置方面遇到了一些问题。

当我运行时:

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,但这还不够。

有人知道如何解决吗?我在网上搜索了很多并没有找到解决方案。

4

1 回答 1

2

所以,我确信这是可行的,尽管我不能说这是一个实际正确的解决方法。

您需要使用户成为超级用户。

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

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

更改用户 ubuntu 超级用户;

退出,它会工作。

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

于 2015-02-16T06:51:40.933 回答