20260422180000 AddNotNullConstraintToKeysOrganizationId migration fails with check constraint error

Summary

GitLab v19.0 included a migration 20260422180000 AddNotNullConstraintToKeysOrganizationId to add a non-null constraint to keys.organization_id.

Several customers have reported errors when upgrading due to there being rows in the keys table with null organization_id values.

The error is:

PG::CheckViolation: ERROR:  check constraint "check_8933ae4f38" of relation "keys" is violated by some row

This looks to be a similar issue to that reported in ERROR: new row for relation "oauth_application... (#600899 - closed)

The workaround of manually running UPDATE keys SET organization_id=1 WHERE organization_id is null; allows the migration to complete.

Can you please check whether there is a code issue allowing values with null organization_id to be inserted into the keys table and whether fixes similar to !242492 (merged) need to be applied for this?

Steps to reproduce

Example Project

What is the current bug behavior?

What is the expected correct behavior?

Relevant logs and/or screenshots

Output of checks

Results of GitLab environment info

Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of: \\\`sudo gitlab-rake gitlab:env:info\\\`)  (For installations from source run and paste the output of: \\\`sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production\\\`)   

Results of GitLab application Check

Expand for output related to the GitLab application check
(For installations with omnibus-gitlab package run and paste the output of: \`sudo gitlab-rake gitlab:check SANITIZE=true\`)  (For installations from source run and paste the output of: \`sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true\`)  (we will only investigate if the tests are passing)  

Possible fixes

Patch release information for backports

If the bug fix needs to be backported in a patch release to a version under the maintenance policy, please follow the steps on the patch release runbook for GitLab engineers.

Refer to the internal "Release Information" dashboard for information about the next patch release, including the targeted versions, expected release date, and current status.

High-severity bug remediation

To remediate high-severity issues requiring an internal release for single-tenant SaaS instances, refer to the internal release process for engineers.

Edited by Justin Farmiloe