Repository navigation
Description
Found by a sweep of release/5.x @ d28c938 for base operations the differs do not account for (the shape of #182); reproduced in a scratch PostgresIntegrationTests case on PostgreSQL 18 (Testcontainers).
Problem
A migration that drops a temporal foreign key's dependent table and also drops or rebuilds the principal's temporal constraint fails at apply with 2BP01.
ApplyTemporalConstraints leaves out the drop of a temporal foreign key whose dependent table the base operations drop, because the key goes with the table. The only exception is when the principal table is dropped too, which is #79:
// NpgsqlComplexIndexMigrationsModelDiffer.cs:783
var pendingForeignKeyDrops = normalizedForeignKeys.Keys
.Where(src => (!droppedTables.Contains(normalizedForeignKeys[src]) || principalDropped.Contains(src))
&& (…));The principal constraint's DropUniqueConstraintOperation is placed before the base operations, so it runs before the base DROP TABLE of the dependent, while the foreign key still references the constraint.
Model: sw1_z_rooms with HasTemporalConstraint(x => x.RoomId, x => x.ValidDuring), and sw1_a_bookings with HasTemporalForeignKey<SwBooking, SwRoom>(x => x.RoomId, x => x.BookedDuring, x => x.RoomId, x => x.ValidDuring). Then remove the booking entity and the room's temporal constraint:
ALTER TABLE sw1_z_rooms DROP CONSTRAINT "AK_sw1_z_rooms_RoomId_ValidDuring";
DROP TABLE sw1_a_bookings;
-- 2BP01: cannot drop constraint AK_sw1_z_rooms_RoomId_ValidDuring on table sw1_z_rooms because other objects depend on itThe same filter catches every other way the principal constraint goes before the base operations: a change to it (DependsOnChangedTemporalConstraint) or a rename that falls back to drop and add (DependsOnRebuiltConstraint, #143).
Change
Drop the foreign key before the base operations whenever its principal constraint is dropped, changed or rebuilt, even when its dependent table is dropped. Apply the DependsOnChangedTemporalConstraint / DependsOnRebuiltConstraint test ahead of the droppedTables exemption. A table drop does not need the key to be there, so dropping it first is always safe.
The migration fails at apply today, so no database holds its result. The fix changes what this model scaffolds, and it ships on release/5.x.
Tests
- A
PostgresIntegrationTestscase for the model above: the migration applies, andpg_constraintandpg_tablesshow both objects gone. - The same with the principal constraint changed (another key column) instead of removed, with the principal on either side of the dependent in EF Core's drop order.
- A
NpgsqlDifftest that theDropForeignKeyOperationcomes before the principal'sDropUniqueConstraintOperation.
Activity
Merge pull request #193 from CaffeinatedCoder/fix/187-188-temporal-fk
CaffeinatedCoder commentedon Oct 23, 2026
Fixed on release/5.x by #193, shipping in 5.6.0. Closed by hand:
Closesacts only on merges intomain.A temporal foreign key is now dropped before the base operations whenever its principal constraint is dropped, changed or rebuilt, also when its dependent table is dropped, so the principal constraint can go (was 2BP01).
docs: resolve the CLAUDE.md merge conflict, and repair the line it ca…