Skip to content

UseComplexIndexes() on PostgreSQL or SQL Server still drops exclusion and temporal constraints and the IS NOT NULL filter without a word #150

Closed
CaffeinatedCoder/CodoMetis.ValueRanges
#335

Description

@CaffeinatedCoder

Found in the 5.5.0 pre-tag review (#36).

#92 made the core differ refuse a model whose complex indexes carry a satellite's index options, because UseComplexIndexes() in place of the satellite's registration (or a missing satellite .targets at design time) applied the rest cleanly without them. The refusal stops at index options. Everything else only a satellite differ does is still dropped without a word, so the shape #92 was a Changed answer for is still open.

Reproduction (934ec55)

PostgreSQL, a context registered with the core method:

public class Grant { public int Id { get; set; } public int GranteeId { get; set; } public NpgsqlRange<DateOnly> Period { get; set; } public string Code { get; set; } = ""; }

protected override void OnModelCreating(ModelBuilder m) => m.Entity<Grant>(b =>
{
    b.ToTable("grants");
    b.HasExclusionConstraint(x => x.GranteeId, x => x.Period, name: "ex_grant_no_overlap");
    b.HasTemporalConstraint(x => x.Code, x => x.Period);
    b.HasComplexIndex(x => x.Code, isUnique: true, indexName: "ux_code");
});

new DbContextOptionsBuilder<Ctx>().UseNpgsql(cs).UseComplexIndexes();   // instead of UseNpgsqlComplexIndexes()
ctx.Database.GenerateCreateScript();
UseComplexIndexes():        EXCLUDE present: False, WITHOUT OVERLAPS present: False, ux_code present: True
UseNpgsqlComplexIndexes():  EXCLUDE present: True,  WITHOUT OVERLAPS present: True,  ux_code present: True

EnsureCreated() applies that script cleanly, and Migrate()'s pending-model-changes check never sees a constraint change.

SQL Server, a unique complex index on an optional complex property (#88's case):

UseComplexIndexes():           CREATE UNIQUE INDEX [ux_vin] ON [cars] ([Registration_Vin]);
UseSqlServerComplexIndexes():  CREATE UNIQUE INDEX [ux_vin] ON [cars] ([Registration_Vin]) WHERE [Registration_Vin] IS NOT NULL;

The core leaves out #88's implicit filter (ImplicitFilter returns null there), so the second row without a registration is refused as a duplicate: #88's bug, back under the core registration.

What it does / what it should do

Proposed fix

In CustomMigrationsModelDiffer.ValidateNoSatelliteOptions (src/EFCore.ComplexIndexes/CustomMigrationsModelDiffer.cs:986), when _satellite is set and the core differ is running, do one of these:

  • Narrow: also refuse any CustomExclusion: / CustomTemporal: declaration on the target model, and on SQL Server any unique complex index that ImplicitFilter would filter.
  • Simpler: refuse every complex-index or constraint declaration, since the satellite's differ is the one that should run.

Keep it target-only, as every check is.

Tests:

  • Npgsql + UseComplexIndexes() + HasExclusionConstraint → GenerateCreateScript() throws, naming UseNpgsqlComplexIndexes().
  • The same for HasTemporalConstraint.
  • SQL Server + UseComplexIndexes() + a nullable unique complex index → throws, naming UseSqlServerComplexIndexes().
  • SQLite with the same declarations → unaffected.

A fix turns a clean EnsureCreated() into a refusal, which is a changed answer. release/5.x cannot ship that after the tag, so it goes in 5.5.0 under ### Changed answers, with its docs/upgrading.md entry beside #92's.

Activity

  1. added this to the 5.5.0 milestone on Oct 20, 2026
  2. added
    bug
    breaking
    silent
    on Oct 20, 2026
  3. added
    severity/high
    package/core
    package/postgresql
    package/sqlserver
    area/indexes
    area/constraints
    area/migrations
    on Oct 20, 2026
  4. CaffeinatedCoder commented on Oct 20, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed in #162 with the narrow variant: the core differ refuses the PostgreSQL satellite's exclusion constraints, temporal constraints and temporal foreign keys, and on SQL Server a unique complex index over nullable columns with no filter. It refuses them only when a satellite serves the provider and the running differ is not the satellite's. That check is merged into #92's, so there is one validation, and it reads the target model only. Each part of the fix was reverted to prove its tests: unit tests, PostgreSQL 18 integration, and the SQL Server 2022 CI lane.

  5. 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/migrationsbreakingbugpackage/corepackage/postgresqlpackage/sqlserverseverity/highsilent

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions