PG::CheckViolation: ERROR: check constraint "check_4fab85ecdc" of relation "ci_build_needs" is violated by some row
Overview
Some environments upgrading to 18.0.X
might encounter this migration error:
PG::CheckViolation: ERROR: check constraint "check_4fab85ecdc" of relation "ci_build_needs" is violated by some row
This can happen if the ci_build_needs
table has rows where the project_id
column is null
.
Environment
-
Impacted offerings:
- GitLab Self-Managed
-
Impacted versions:
- GitLab 18.0.1
Workaround
- Open a database console.
- Check if there are rows violating this constraint.
select count(*) from ci_build_needs where project_id is null; select * from ci_build_needs where project_id is null limit 1;
- If one or more rows are affected, we can backfill them manually using the following query.
UPDATE ci_build_needs SET project_id = ci_builds.project_id FROM ci_builds WHERE ci_build_needs.build_id = ci_builds.id AND ci_build_needs.project_id IS NULL;
- Validate that there are no longer any affected rows.
select count(*) from ci_build_needs where project_id is null;
- Re-attempt the migration by running
gitlab-rake db:migrate
.
Cause
- A migration was introduced in
17.4
wherein it's possible that this was missed earlier during an upgrade path, or some of relevant rows were not back-filled completely.