Repository navigation
fix: create declarations on a TPC type on every table of the hierarchy (#93) - #131
Merged
Merged
Conversation
#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
force-pushed
the
fix/93-tpc-concrete-base
branch
from
October 20, 2026 08:47
3f61b5c to
a6a91b6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
HasIndexon the same base is created on every concrete table. The migration scaffolded and applied clean, and on PostgreSQL 18 an overlapping booking was refused inbookingsand accepted inspecial_bookings; a unique complex index admitted duplicates among the derived types' rows.What EF Core 10 does natively (measured)
HasIndexon 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 repeatsix_roomon 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.ToJson()alike:special_bookingshas noContact_EmailorContactcolumn. Saving a derived instance drops the value silently, and querying the derived type throwsKeyNotFoundException. On the derived table,GetColumnName(storeObject)forContact.Emailreturns"Email", the bare property name and not a column of that table.Change
Option 1, emit per table, as EF does, with a refusal for the two shapes that can't be copied. Reasons:
HasIndex, and leaves nothing silent. Option 2 would turn a common, valid model (a unique code on a concrete base) into an error with no workaround except moving the declaration onto every leaf, which isn't possible for the base's own table.Files:
CustomMigrationsModelDiffer.cs:DeclarationTablesreturns 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.ThrowIfMissingFromDerivedTablerefuses 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, notGetColumnName, which returns"Email"there: on a derived type with its ownEmailproperty, that column would have been indexed instead.TpcDeclarationis 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".ThrowIfDeclaredOnUnmappedTypenow fires only when no table of the hierarchy holds the rows.DeclarationTablesapplies the per-table rule only atCustomIndex: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 firstmigrations addcreates 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.ValidateNamesAcrossKindsgives repeated explicit names the TPC message.HasIndexdoes, 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 answersrows (Indexes, Constraints), three### Breakingrows (complex member, repeated explicit name, FK to a multi-table principal) and a### Changedrow (abstract base), in area order.docs/upgrading.mdgets a "Declarations on a TPC type cover every table of the hierarchy" entry under Changed answers in 5.5.0, which says what the firstmigrations addcreates 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_codeexists ongift_vouchersafter (read fromsys.indexes), and the duplicate is refused. It was proven on CI on a throwawayproof/93-tpc-sqlserverbranch, 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:
DeclarationTablesreturns the own table)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