Skip to content

A complex-index rename runs after a native CREATE INDEX that takes its old name (42P07) #143

Closed

Description

@CaffeinatedCoder

Found by a pre-release sweep of main @ 934ec55; reproduced in a scratch test.

Problem

Index renames run after the base operations (CustomMigrationsModelDiffer.cs:309). When a native HasIndex in the target takes the name a complex index is renamed away from, EF's CREATE INDEX runs while the old index still holds that name. ValidateNoNativeIndexNameCollision checks target names only, so it passes.

Model: v1 HasComplexIndex(x => x.Email.Value, indexName: "ix_a"); v2 HasComplexIndex(x => x.Email.Value, indexName: "ix_b") plus HasIndex(x => x.Code).HasDatabaseName("ix_a").

CREATE INDEX ix_a ON members ("Code");   -- 42P07: ix_a still exists
ALTER INDEX ix_a RENAME TO ix_b;

Exclusion and temporal constraint renames are built the same way (NpgsqlComplexIndexMigrationsModelDiffer.cs:726-760, :893-910): traced, not run.

Change

When a rename's old name is taken by a base CreateIndexOperation (or a constraint add, for the constraint renames), fall back to dropping first and creating last.

Tests

An NpgsqlDiff test: no RenameIndexOperation named ix_a after the CreateIndexOperation named ix_a. The same for an exclusion constraint rename against a native unique constraint taking its old name.

Outcome (PR #148)

The constraint renames reproduced: on PostgreSQL 18, an exclusion or temporal constraint renamed away from a name that a native unique constraint takes fails with 42P07. Two more shapes have the same cause and are fixed in the same PR. First, two complex indexes swapping names, where RENAME ix_c TO ix_b runs while ix_b still exists. Second, a temporal constraint that falls back to drop-and-add must take its temporal foreign keys with it, or the drop fails with 2BP01 (verified live). The old name is checked against the target model, not against the operations: anything the target gives that name to, in the namespace the provider uses.

Activity

  1. added this to the 5.5.0 milestone on Oct 20, 2026
  2. added
    bug
    severity/medium
    package/core
    package/postgresql
    area/indexes
    area/constraints
    on Oct 20, 2026
  3. added
    area/migrations
    on Oct 20, 2026
  4. added a commit that references this issue on Oct 20, 2026
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/indexesarea/migrationsbugpackage/corepackage/postgresqlseverity/medium

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions