Skip to content

fix: create declarations on a TPC type on every table of the hierarchy (#93) - #131

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/93-tpc-concrete-base
Oct 20, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/93-tpc-concrete-base

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #93

Problem

In a TPC hierarchy with a concrete base, a complex index, exclusion constraint, temporal constraint or temporal foreign key declared on the base was created on the base's table only. EF Core's own HasIndex on the same base is created on every concrete table. The migration scaffolded and applied clean, and on PostgreSQL 18 an overlapping booking was refused in bookings and accepted in special_bookings; a unique complex index admitted duplicates among the derived types' rows.

What EF Core 10 does natively (measured)

  • HasIndex on a concrete TPC base lands on every concrete table below it, at every level: IX_bookings_Code, IX_special_bookings_Code, IX_vip_bookings_Code. Default names carry the table, so they stay unique per schema.
  • HasDatabaseName("ix_room") on the base repeats ix_room on every table. EF does not check it: on SQLite the migration fails at apply with "index ix_room already exists", and on PostgreSQL it would fail with 42P07.
  • Complex properties of a TPC type are left out of every derived table, table-split and ToJson() alike: special_bookings has no Contact_Email or Contact column. Saving a derived instance drops the value silently, and querying the derived type throws KeyNotFoundException. On the derived table, GetColumnName(storeObject) for Contact.Email returns "Email", the bare property name and not a column of that table.
  • A relationship whose principal is a TPC type with derived types gets no foreign key at all.

Change

Option 1, emit per table, as EF does, with a refusal for the two shapes that can't be copied. Reasons:

Files:

  • CustomMigrationsModelDiffer.cs: DeclarationTables returns the type's table and, under TPC, each derived type's table, each resolved against that table's entity type. Index descriptors are built per table. ThrowIfMissingFromDerivedTable refuses a declaration whose parts, template or filter placeholders or INCLUDE entries reach into a complex property that the derived table has no column for. It checks the table's column mappings, not GetColumnName, which returns "Email" there: on a derived type with its own Email property, that column would have been indexed instead. TpcDeclaration is shared by the copies of one declaration and kept out of equality, so an explicit name that repeats gets a message about the TPC copies instead of "two declarations collide". ThrowIfDeclaredOnUnmappedType now fires only when no table of the hierarchy holds the rows.
  • The rendering-version gate. Both diff sides are resolved with the same rule, so without a gate an existing snapshot would resolve to the copies too and no migration would ever create them, the trap 5.4.0 hit with JSON rendering. DeclarationTables applies the per-table rule only at CustomIndex:RenderingVersion ≥ 3, which is 5.5.0's (from SQL Server: a unique complex index over nullable columns admits only one row without a value #88). A pre-5.5.0 snapshot resolves to the base table, so the first migrations add creates exactly the derived copies and drops nothing.
  • NpgsqlExclusionConstraintExtensions, NpgsqlExclusionMutableExtensions, NpgsqlTemporalConstraintExtensions: exclusion and temporal declarations now mark the rendering version, since a model with only constraints carried none and would have stayed on the old rule for good.
  • NpgsqlComplexIndexMigrationsModelDiffer.cs: exclusion, temporal and temporal-FK descriptors are built per table. A temporal FK from a TPC base references the principal from every dependent table. A temporal FK to a TPC type with derived types is refused (PrincipalTable): it used to reference the base table only and refused every reference to a derived row. An abstract principal with a single concrete table resolves to that table. ValidateNamesAcrossKinds gives repeated explicit names the TPC message.
  • SQL Server inherits the core path. Index names there are per table, so an explicit name repeats on each table, as EF's does. The clustered-slot and key-name checks (SQL Server: a second clustered complex index in a later migration, and an index named like the table's key, fail only at apply #90) see each copy on its own table.
  • The abstract TPC base, refused since 5.1.0, now gets its declarations on every concrete table, as EF's HasIndex does, unless they read a complex member, which no concrete table has a column for.

Changelogs: the root and the core and PostgreSQL package changelogs get two ### Changed answers rows (Indexes, Constraints), three ### Breaking rows (complex member, repeated explicit name, FK to a multi-table principal) and a ### Changed row (abstract base), in area order. docs/upgrading.md gets a "Declarations on a TPC type cover every table of the hierarchy" entry under Changed answers in 5.5.0, which says what the first migrations add creates and that rows violating the new copies must be removed first (23505, 23P01, Msg 1505). The 5.5.0 introduction stays count-free. CLAUDE.md's section on declarations without a table now describes the rule.

Tests

  • TpcHierarchyTests (34 cases) covers each kind on a three-level concrete hierarchy: complex index on SQLite, PostgreSQL and SQL Server, composite with a placeholder filter, expression index, exclusion constraint, temporal constraint, and temporal FK from the base. For an FK to a multi-table principal it expects a refusal, and for one to a leaf the leaf's table. TPH stays a single index. The name tests are an explicit name refused on PostgreSQL and SQLite, accepted on SQL Server, and accepted on PostgreSQL across three schemas; a leaf declaration named like a base copy (core check and cross-kind check); and the identifier limit on a copy's default name. Complex-member refusals are covered for table-split, property-level, ToJson() and a filter placeholder. Upgrade tests check no churn against itself and that a pre-5.5.0 model creates or adds exactly the derived copies, for indexes and for a constraint-only model.
  • SnapshotRoundTripTests: a compiled, real snapshot of a TPC model with every kind round-trips with no churn, and with the version key stripped it creates exactly the six derived constraint copies and the two index copies.
  • UnmappedDeclarationTests: updated for the abstract base. Scalar declarations land on every concrete table, complex-member ones are refused with the new message, and a base whose only concrete type maps to a view still fails.
  • PostgresIntegrationTests.Tpc_derived_table_enforces_after_upgrade (PostgreSQL 18) applies the pre-5.5.0 answer and shows the derived table accepting an overlap the base refuses. The upgrade migration fails over those rows with 23P01 and, in its transaction, applies nothing. After the cleanup it applies, and the derived table then refuses the overlap (23P01) and a duplicate code (23505).
  • SqlServerIntegrationTests.Tpc_derived_table_enforces_after_upgrade (SQL Server 2022) runs the same upgrade with a unique index: the duplicate is accepted before, ux_voucher_code exists on gift_vouchers after (read from sys.indexes), and the duplicate is refused. It was proven on CI on a throwaway proof/93-tpc-sqlserver branch, now deleted: run 39394857606 passed, and run 39395251480, with the per-table rule reverted, failed with "Index 'ux_voucher_code' does not exist on table 'gift_vouchers'".

Counterfactuals, each reverted alone against the unit suite plus the PostgreSQL 18 test:

Reverted Failing tests
Per-table rule (DeclarationTables returns the own table) 36, the PostgreSQL 18 integration test among them
Rendering-version gate 6 (every upgrade test, both snapshot and live)
Missing-column check on derived tables 7
TPC repeated-name message 4
FK-to-multi-table-principal refusal 1
Version marking on exclusion and temporal declarations 10

Full local suite: 549 passed, PostgreSQL 16 and 18 integration included; SQL Server ran on CI.

Self-review along migration-safety-review: the snapshot round trip and upgrade rollout are covered above. Operation order is unchanged: copies are ordinary descriptors, so drops still run before the base operations and creates after them. Dropped and renamed derived tables follow the existing per-table normalization. The runtime differ (EnsureCreated) shares the code. Provider scoping is unchanged.

Note for #95: the SQLite rebuild guard (ThrowIfSqliteRebuildDropsIndexes) reads the target descriptors, so it now also refuses a rebuild of a derived TPC table that carries a copy of a base declaration.

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred severity/medium Inconsistency or failure in a narrow scenario package/core Affects EFCore.ComplexIndexes package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL area/indexes Complex-property, composite, unique and filtered indexes area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints breaking Changes an answer or a signature — needs a major labels Oct 20, 2026
#93)

On a concrete TPC base, complex indexes, exclusion constraints, temporal
constraints and temporal foreign keys landed on the base's table only,
while EF Core creates its own HasIndex on every concrete table. On
PostgreSQL 18 an overlapping booking was refused in the base table and
accepted in the derived one; the migration applied clean.

Every descriptor scan now goes through DeclarationTables: the type's own
table and, under TPC, the table of each derived type, each resolved
against that table's entity type. The rule is gated on rendering version
3, so a pre-5.5.0 snapshot resolves to the base table only and the first
migrations add creates the derived copies; exclusion and temporal
declarations now mark the version too, since a constraint-only model
carried none.

Refused instead of copied:
- a declaration reading a complex-property member: EF Core 10 maps no
  column for it on a derived table, and GetColumnName there returned the
  bare property name ("Email" for Contact.Email);
- a temporal FK to a TPC type with derived types, whose rows span
  tables (EF Core creates no FK for such a relationship either);
- an explicit name repeated across copies where names are per schema or
  database, with a message naming the TPC copies.

The abstract TPC base, refused since 5.1.0, now gets its declarations on
every concrete table unless they read a complex member.

Verified by reverting each piece: per-table rule (36 tests fail, the
PostgreSQL 18 integration test among them), version gate (6), missing-
column check (7), TPC name message (4), FK principal check (1), version
marking on constraints (10). The SQL Server 2022 lane passed on a proof
branch and failed there with the per-table rule reverted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder force-pushed the fix/93-tpc-concrete-base branch from 3f61b5c to a6a91b6 Compare October 20, 2026 08:47
@CaffeinatedCoder
CaffeinatedCoder merged commit a77db7c into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/93-tpc-concrete-base branch October 20, 2026 08:50
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/indexes Complex-property, composite, unique and filtered indexes breaking Changes an answer or a signature — needs a major bug Something isn't working package/core Affects EFCore.ComplexIndexes package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TPC with a concrete base: complex indexes, exclusion and temporal constraints land on the base table only

1 participant