Louis Simoneau

Ruby, OOP, TDD, Remote Work, Miscellany

PostgreSQL System Trigger Error With Rails

PG::Error: ERROR:  permission denied: "RI_ConstraintTrigger_50931" is a system trigger

Just ran into this issue and had a hard time locating a solution, so just throwing it up here so others might be able to find it faster.

You might see this error message if:

  • You’re using PostgreSQL with Rails
  • You’re using foreign key constraints
  • You’re using database_cleaner to tear down your test databases

The problem is this: Postgres sets up foreign key constraints as “system triggers,” which can only be removed by a superuser. When database_cleaner tries to empty out your database between test runs, it tries to remove these keys, which causes the failure.

Currently there isn’t a “real” fix for this, but Sergey Potapov has bundled together the required hacks into a gem called rails3_pg_deferred_constraints. Pop it into your test group and you should be good to go.