Skip to content

Factory seeding fails with duplicate key value violates unique constraint "index_ci_namespace_mirrors_on_namespace_id"

Summary

Some factories are failing with PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_ci_namespace_mirrors_on_namespace_id".

The workaround is to retry factory creation.

Steps to reproduce

  1. Enter Rails console gdk rails console
  2. Create FactoryBot.create(:board_group_recent_visit)
FactoryBot.create(:board_group_recent_visit)
# factory is created
=> #<BoardGroupRecentVisit:0x000000028223f508
 id: 1,
 created_at: Wed, 28 Feb 2024 17:25:45.371008000 UTC +00:00,
 updated_at: Wed, 28 Feb 2024 17:25:45.371008000 UTC +00:00,
 user_id: 70,
 board_id: 1,
 group_id: 96>
  1. Create the same factory once more
FactoryBot.create(:board_group_recent_visit)
# on second try failed with the same `index_ci_namespace_mirrors_on_namespace_id`
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_ci_namespace_mirrors_on_namespace_id"
DETAIL:  Key (namespace_id)=(100) already exists.
  1. Repeat factor creation
FactoryBot.create(:board_group_recent_visit)
# didn't fail on third time
=> #<BoardGroupRecentVisit:0x0000000128e7ec48
 id: 2,
 created_at: Wed, 28 Feb 2024 17:26:11.482766000 UTC +00:00,
 updated_at: Wed, 28 Feb 2024 17:26:11.482766000 UTC +00:00,
 user_id: 73,
 board_id: 2,
 group_id: 102>

Other example failures can be seen in https://gitlab.com/gitlab-org/quality/pg-dump-generator/-/jobs/6393739616 by searching PG::UniqueViolation

Edited by Alex Ives