Skip to content

fix: drop a temporal foreign key first when its principal table is dropped (#79) - #102

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/79-temporal-fk-drop-order
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/79-temporal-fk-drop-order

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #79

Problem

A migration that drops both the principal table of a temporal foreign key and its dependent table failed at apply whenever the principal's table sorted first:

model: a_rooms (HasTemporalConstraint) ← b_bookings (HasTemporalForeignKey to a_rooms)
DROP TABLE a_rooms;      -- 2BP01 cannot drop table a_rooms because other objects depend on it
DROP TABLE b_bookings;

ApplyTemporalConstraints dropped a temporal foreign key only when its dependent table survived, and EF orders DropTable operations without knowing about the key. The Down() of the migration that created both tables has the same shape, so database update <previous> and migrations remove after applying could not roll back. Whether it failed depended only on the table names.

Change

  • NpgsqlComplexIndexMigrationsModelDiffer.ApplyTemporalConstraints: a source foreign key whose principal table (by its source identity, before any rename) is dropped is now dropped explicitly, before the base operations, even when its dependent table is dropped too. A dependent dropped alone still takes the key with it (the existing "Dropping the dependent table skips explicit temporal FK drop" test still holds).
  • Changelog: one ### Fixed line in the root and PostgreSQL changelogs.

Tests

  • NpgsqlTemporalForeignKeyDifferTests.Dropping_both_tables_drops_foreign_key_first_when_principal_sorts_first: diffs a model with a_subscriptions ← b_addons to nothing, asserts EF drops a_subscriptions first (the premise) and that a DropForeignKeyOperation on b_addons precedes the first DropTableOperation.
  • PostgresIntegrationTests.Temporal_foreign_key_tables_drop_together: creates ig_a_rooms ← ig_b_reservations with a row in each, applies the drop-everything diff with the stock generator, and checks both tables are gone.
  • Reverted the fix: the differ test fails ("must be dropped explicitly") and the integration test fails with 2BP01: cannot drop table ig_a_rooms because other objects depend on it.
  • Full suite, integration included: 400 passed.

🤖 Generated with Claude Code

…opped

A migration dropping both tables of a temporal foreign key skipped the key,
since its dependent table was going anyway, and EF ordered the DropTable
operations without knowing about it. When the principal's table sorted first
the migration failed with 2BP01; Down() of the migration that created the
two tables has the same shape, so it could not roll back either.

The key is now dropped before the base operations whenever its principal
table is dropped. Proven by reverting: the differ test finds no
DropForeignKeyOperation and the integration test fails with 2BP01 on
PostgreSQL 18.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working severity/medium Inconsistency or failure in a narrow scenario package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints area/migrations The differs, snapshot format, EnsureCreated and design-time wiring labels Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit e8ac030 into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/79-temporal-fk-drop-order branch October 20, 2026 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints area/migrations The differs, snapshot format, EnsureCreated and design-time wiring bug Something isn't working package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dropping both tables of a temporal foreign key fails when the principal sorts first, Down() included

1 participant