Skip to content

Conversation

@7ttp
Copy link
Contributor

@7ttp 7ttp commented Dec 6, 2025

summary

this pr fixes an issue where supabase db diff generates incorrect migrations for partitioned tables. when a foreign key references a partitioned table, postgresql automatically creates inherited constraints with numeric suffixes (fkey1, fkey2, etc.) for each partition. migra sees these as separate constraints and generates drop/add statements that fail with:

error: cannot drop inherited constraint table_name_fkey1 of relation table_name

problem

when using declarative schemas with partitioned tables:

  1. postgresql creates inherited fk constraints for each partition (users_photo_fkey1, users_photo_fkey2, etc.)
  2. migra treats these as independent constraints
  3. the generated migration tries to drop inherited constraints (which postgresql forbids)
  4. users gets duplicate constraint operations

solution

added a filter that removes alter table statements operating on constraints matching the pattern fkey followed by digits. this pattern is exclusively used by postgresql for auto-generated inherited constraints. legitimate user-defined constraints (like users_id_fkey without numeric suffix) are preserved.

the fix is applied to both the migra and pgadmin diff paths.

testing

added 7 test cases covering:

  • filtering inherited fkey constraints
  • preserving non-inherited constraints
  • empty input handling
  • mixed statements with partitioned table constraints
  • exact bug report pattern matching

closes #4562

@7ttp 7ttp requested a review from a team as a code owner December 6, 2025 11:04
when a foreign key references a partitioned table, postgresql automatically
creates inherited constraints with numeric suffixes (fkey1, fkey2, etc.) for
each partition. migra treats these as independent constraints and generates
drop/add statements for them, which fails because postgresql forbids dropping
inherited constraints directly.

this change filters out alter table statements that operate on constraints
matching the pattern fkey followed by digits, as these are always postgresql
auto-generated inherited constraints. legitimate user-defined constraints
(like users_id_fkey without numeric suffix) are preserved.

the fix is applied to both the migra and pgadmin diff paths to ensure
consistent behavior across all diff methods.

closes supabase#4562
@7ttp 7ttp changed the base branch from main to develop December 6, 2025 11:15
@sweatybridge
Copy link
Contributor

We won't be accepting PRs to patch specific diff tools as that work should be done upstream, ie. migra.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

supabase db diff generates incorrect migrations for partitioned tables

2 participants