Step 3: Add composite foreign keys with partition_id for vulnerability_identifiers references
Summary
Add partition_id to foreign keys that reference vulnerability_identifiers, using composite FKs with on_update: :cascade.
Two tables reference vulnerability_identifiers:
| Source table | Source column | FK name | On delete | Status |
|---|---|---|---|---|
vulnerability_occurrence_identifiers |
identifier_id |
fk_rails_be2e49e1d0 |
CASCADE | fk_rails_be2e49e1d0_p |
vulnerability_occurrences |
primary_identifier_id |
fk_rails_c8661a61eb |
CASCADE | fk_rails_c8661a61eb_p |
Current database state (verified 2026-08-19)
vulnerability_occurrence_identifiershas apartition_idcolumn (DEFAULT 1) with a validated NOT NULL check constraint (check_aacd1ff57e)✅ vulnerability_occurrences.partition_idhas a validated NOT NULL check constraint (check_3225d02bda)✅ vulnerability_identifiershas apartition_idcolumn (DEFAULT 1, nullable) with unique index(id, partition_id)✅ - Composite FK
fk_rails_be2e49e1d0_p(partition_id, identifier_id)→vulnerability_identifiers(partition_id, id)withON UPDATE CASCADE ON DELETE CASCADEexists and is validated✅ - Old single-column FK
fk_rails_be2e49e1d0has been removed✅ - Composite FK
fk_rails_c8661a61eb_p(partition_id, primary_identifier_id)→vulnerability_identifiers(partition_id, id)withON UPDATE CASCADE ON DELETE CASCADEexists and is validated (addedNOT VALIDin !244081 (merged), async validation in !247860 (merged), sync validation in !247863 (merged))✅ - Supporting index
idx_vuln_occurrences_on_partition_id_primary_identifier_idonvulnerability_occurrences (partition_id, primary_identifier_id)created (async !244112 (merged), sync !244113 (merged))✅ - Old single-column FK
fk_rails_c8661a61ebonvulnerability_occurrences.primary_identifier_idhas been removed (!247863 (merged))✅ vulnerability_occurrence_identifiersalso has FKfk_rails_e4ef6d027c→vulnerability_occurrences(id)ON DELETE CASCADE
Steps
For vulnerability_occurrence_identifiers: ✅ Complete
Add— !237458 (merged), !240979 (merged), !242871 (merged)partition_idcolumn withDEFAULT 1, NOT NULLtovulnerability_occurrence_identifiersAdd composite FK— !237458 (merged)(partition_id, identifier_id)→vulnerability_identifiers(partition_id, id)withon_update: :cascade, on_delete: :cascadeValidate the FK— async in !237458 (merged), sync in !240976 (merged)Remove old single-column FK— !240976 (merged)fk_rails_be2e49e1d0onidentifier_id
For vulnerability_occurrences: ✅ Complete
Add NOT NULL constraint on— done via !240991 (merged) and !242869 (merged)partition_idAdd composite FK— !244081 (merged) (merged 2026-08-04)(partition_id, primary_identifier_id)→vulnerability_identifiers(partition_id, id)withon_update: :cascade, on_delete: :cascade(NOT VALID)Add supporting index on— async !244112 (merged), sync !244113 (merged)(partition_id, primary_identifier_id)Validate the FK— async validation in !247860 (merged), sync validation in !247863 (merged) (merged 2026-08-19)Remove old single-column FK— !247863 (merged) (merged 2026-08-19)fk_rails_c8661a61ebonprimary_identifier_id
Reference
- List partitioning docs — Step 4: Enforce foreign key constraint
- Example:
db/post_migrate/20260407120144_create_missing_fk_and_analyze_p_ci_build_needs.rb - Completed example for this issue: MRs !237458 (merged), !240976 (merged), !240979 (merged), !240991 (merged), !242869 (merged), !242871 (merged)
Notes
- The
on_update: :cascadeis mandatory — without it, updating the partition column would fail with referential integrity errors - This step depends on the unique index
(id, partition_id)onvulnerability_identifiersalready being in place (✅ done) vulnerability_identifiers.partition_idis still nullable — ensure this is addressed in the partitioning work for the referenced table- The
vulnerability_occurrence_identifierstable also has a FKfk_rails_e4ef6d027ctovulnerability_occurrences(id)— ifvulnerability_occurrencesis also being partitioned, the FKs there may need to be updated in coordination
Acceptance criteria
-
vulnerability_occurrence_identifiershaspartition_idcolumn (DEFAULT 1, NOT NULL) -
vulnerability_occurrences.partition_idhas NOT NULL constraint - Composite FK from
vulnerability_occurrence_identifiers(partition_id, identifier_id)→vulnerability_identifiers(partition_id, id)exists and is validated - Composite FK from
vulnerability_occurrences(partition_id, primary_identifier_id)→vulnerability_identifiers(partition_id, id)exists and is validated (addedNOT VALIDin !244081 (merged), validated via !247860 (merged) / !247863 (merged)) - Old single-column FKs are removed (
fk_rails_be2e49e1d0removed via !240976 (merged),fk_rails_c8661a61ebremoved via !247863 (merged))
Edited by Michał Zając