Skip to content

Run test PG::ForeignKeyViolation: ERROR: insert or update on table "employees" violates foreign key constraint

Victoriano edited this page Jun 25, 2022 · 2 revisions

PG::ForeignKeyViolation: ERROR: insert or update on table "employees" violates foreign key constraint

Error:
EmployeesControllerTest#test_can_not_get_employees_index_without_authenticate:
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "employees" violates foreign key constraint "fk_rails_b82b548cdd"
DETAIL:  Key (store_id)=(1) is not present in table "stores".

or

Error:
EmployeesControllerTest#test_should_delete_an_employee:
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "employees" violates foreign key constraint is not present in table "stores".

The error arises because the postgresql database does not have administrator [root] permissions.

Two options to solve it. (Linux)

linux
  1. From postgresql as root -> terminal.

    1.1 You create a new user. 1.2 You grant administrator permission.

  2. Work directly, as postgresql root.