How to work with tests and fixtures in a database with foreign keys in Rails?

I use Rails 4.1 and PostgreSQL, and I use the alien character to create foreign key constraints. But I am having problems with fixtures .

When I run:

spring rake test 

I got the following errors:

 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: recreate the database:

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

I get some errors when trying to seed a development database using tools with rake db:fixture:load .

I already tried to change the boot order of the device in test_helper.rb , but this was not enough.

Does anyone know how to fix this? I searched a lot on the Internet and did not find a solution.

+5
source share
1 answer

So, I know for sure that this works, although I cannot say that this is really the right job.

You need to do SUPERUSER.

In my case using Cloud 9, the user I signed up with is ubuntu.

I enter sudo -u postgres psql (actually sudo twice to access C9) and type:

ALTER USER ubuntu SUPERUSER;

Logout and it will work.

If you run tail log / test.rb, you can see PG :: InsufficientPrivlege: ERROR when running the integration test so that the user does not have sufficient permissions. You would not want to do this in production, but my production is in Heroku, so I am good.

+2
source

Source: https://habr.com/ru/post/1201658/


All Articles