Skip to content

Sporadic rspec failures due to PG::UniqueViolation

This has happened to me on several occasions across different MRs and different spec files.

Most recently, I did the following rspec command: rspec spec/features/merge_requests/filter_merge_requests_spec.rb which resulted in the following:

1) Filter merge requests for assignee from mr#index assignee does not change when closed link is clicked
     Failure/Error: let!(:project)   { create(:project) }

     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_routes_on_source_type_and_source_id"
       DETAIL:  Key (source_type, source_id)=(Namespace, 2) already exists.
       : INSERT INTO "routes" ("source_type", "path", "name", "source_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING “id”
#
# ....
# ------------------
     # --- Caused by: ---
     # PG::UniqueViolation:
     #   ERROR:  duplicate key value violates unique constraint "index_routes_on_source_type_and_source_id"
     #   DETAIL:  Key (source_type, source_id)=(Namespace, 2) already exists.
     #   /.rvm/gems/ruby-2.3.2/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:602:in `exec_prepared'