Skip to content

Dropping a temporal foreign key's dependent table together with the principal's temporal constraint fails (2BP01) #187

Closed

Description

@CaffeinatedCoder

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 it

The 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 PostgresIntegrationTests case for the model above: the migration applies, and pg_constraint and pg_tables show 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 NpgsqlDiff test that the DropForeignKeyOperation comes before the principal's DropUniqueConstraintOperation.

Activity

  1. added this to the 5.6.0 milestone on Oct 23, 2026
  2. added
    bug
    severity/medium
    package/postgresql
    area/constraints
    area/migrations
    on Oct 23, 2026
  3. added a commit that references this issue on Oct 23, 2026
  4. CaffeinatedCoder commented on Oct 23, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed on release/5.x by #193, shipping in 5.6.0. Closed by hand: Closes acts only on merges into main.

    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).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/constraintsarea/migrationsbugpackage/postgresqlseverity/medium

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions