简体   繁体   English

如何使用Rails中具有外键约束的数据库中的测试和装置?

[英]How to work with tests and fixtures in a database with foreign key contraints in Rails?

I am using Rails 4.1 and PostgreSQL and I am using the foreigner gem to create foreign key constraints. 我正在使用Rails 4.1和PostgreSQL,我使用外国人gem来创建外键约束。 But I am having some problems with fixtures . 但是我遇到了一些固定装置的问题。

When I run: 当我跑:

spring rake test

I got errors like the following: 我收到如下错误:

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"

I found a workaround: recreating the database: 我找到了一个解决方法:重新创建数据库:

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

I get the some kind of errors when I try to seed the development database with fixtures using rake db:fixture:load . 当我尝试使用rake db:fixture:load尝试使用fixtures为开发数据库播种时,我遇到了一些错误。

I already tried to change the fixture loading order in test_helper.rb , but it wasn't enough. 我已经尝试在test_helper.rb更改夹具加载顺序,但这还不够。

Someone know how to fix it? 有人知道如何解决它吗? I searched a lot in the web and didn't found a solution. 我在网上搜索了很多,但没有找到解决方案。

So, I know for sure this works, though I can't say it's an actual correct work around. 所以,我肯定知道这是有效的,但我不能说这是一个实际的正确的工作。

You need to make the user a SUPERUSER. 您需要使用户成为SUPERUSER。

In my case using Cloud 9, the user I'm logged in as is "ubuntu". 在我使用Cloud 9的情况下,我登录的用户是“ubuntu”。

I enter sudo -u postgres psql (actually sudo twice for C9 to access correctly) and type: 我输入sudo -u postgres psql(实际上sudo两次让C9正确访问)并输入:

ALTER USER ubuntu SUPERUSER; ALTER USER ubuntu SUPERUSER;

Quit out and it will work. 退出,它会工作。

If you run tail log/test.rb you can see a PG::InsufficientPrivlege: ERROR when you run your integration test, so the user doesn't have sufficient rights. 如果运行tail log / test.rb,则在运行集成测试时可以看到PG :: InsufficientPrivlege:ERROR,因此用户没有足够的权限。 You would not want to do this in production, but my production is on Heroku so I'm good. 你不想在制作中这样做,但我的作品是在Heroku上,所以我很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM