Skip to content

Drop a temporal foreign key before the base operations that need it gone (#187, #188) - #193

Merged
CaffeinatedCoder merged 2 commits into
release/5.xfrom
fix/187-188-temporal-fk
Oct 23, 2026
Merged

CaffeinatedCoder merged 2 commits into
release/5.xfrom
fix/187-188-temporal-fk

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #187
Closes #188

Into release/5.x. Both are temporal-FK ordering bugs in ApplyTemporalConstraints, so they share one PR, as #167/#168 did. Closes acts only on main, so both issues are closed by hand after the merge.

Problem

Two base operations met a temporal foreign key that the differ had left in place, and the migration failed when it ran.

Change

In NpgsqlComplexIndexMigrationsModelDiffer.ApplyTemporalConstraints:

Tests

  • TemporalForeignKeyOrderingTests (unit):
    • Dropping the dependent table together with the principal constraint drops the key, then the constraint, then the table.
    • The same with the principal constraint changed to another key.
    • Dropping the dependent table alone still leaves the key to the table.
    • A key type change on both tables drops the key before both ALTER COLUMNs and adds it after, without rebuilding the principal constraint.
    • A type change on a column the key doesn't read leaves it alone.
  • PostgreSQL 18 integration:
    • Dropping_dependent_table_and_principal_constraint_applies, with the dependent sorting first, as in the issue.
    • Key_type_change_on_both_sides_applies, in both orders of the table names, with a row in each table. Afterwards the key exists and still rejects an orphan booking (23503).
  • With the source fix reverted: the three unit tests fail, and the integration tests fail with exactly the issues' errors: 2BP01: cannot drop constraint AK_tk1_z_rooms_RoomId_ValidDuring … and 42804: foreign key constraint "FK_tk2_z_bookings_tk2_a_rooms_RoomId_BookedDuring" cannot be implemented.
  • Suites: unit plus PostgreSQL 18/16, 723 passed.

🤖 Generated with Claude Code

… it gone

Two base operations met a temporal foreign key the differ had left in
place, and the migration failed at apply.

A key whose dependent table the base operations drop was left to go with
the table, also when its principal constraint was dropped or changed.
That drop runs before the base operations, while the key still
referenced it (2BP01, #187). The key is now dropped first whenever its
principal constraint is dropped, changed or rebuilt.

A key column's type changed on both tables: PostgreSQL re-checks the key
after each ALTER COLUMN, and a GiST key has no cross-type operator, so a
dependent altered before its principal failed (42804, #188). A type
change on a key column on either side is now in the way of the key
(RetypedColumns), so it is dropped before the base operations and added
after.

Closes #187
Closes #188

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.6.0 milestone Oct 23, 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 23, 2026
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.

1 participant