Skip to content

Shorten the default names of TPC copies over the identifier limit - #149

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/145-tpc-copy-names
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/145-tpc-copy-names

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #145

Problem

On PostgreSQL, a declaration on a TPC type whose derived table has a long name could not be named at all. With Booking TPC on bookings, HasComplexIndex(x => x.Code, isUnique: true) and SpecialBooking on special_bookings_for_corporate_customer_accounts_xyz_1234 (57 characters):

  • the default name IX_special_bookings_for_corporate_customer_accounts_xyz_1234_Code (65 bytes) was refused by ThrowIfIdentifierTooLong, whose message said to give an explicit, shorter name;
  • an explicit name was refused by TpcDeclaration.RepeatedName, since it repeats on every table, and that message said to leave the name out.

Exclusion constraints (EX_…_RoomId_During) and temporal constraints (AK_…) hit the same dead end. EF Core's own HasIndex on the same model scaffolds, because EF shortens the name.

Change

Decision on the issue: shorten TPC copies only.

  • CustomMigrationsModelDiffer.ShortenIdentifier shortens a name the way EF Core 10's Uniquifier.Uniquify/Truncate does (decompiled from 10.0.0): cut to the limit and end with ~, then ~1, ~2, … for as long as the result is taken. It measures through MeasureIdentifier, so on PostgreSQL the cut is at 63 bytes and at a whole character. For ASCII names the result matches EF's exactly.
  • IsLongDefaultCopyName limits shortening to the default name of a copy whose declaration has a TpcDeclaration origin (more than one table), when that name is over GetMaxIdentifierLength. Every other default name keeps the 5.2.0 refusal.
  • Core (ExtractAllIndexDescriptors → ShortenIndexCopyNames): names are made unique under the provider's IndexNameScope/IndexNameComparer, against this package's other complex indexes and EF's own indexes and keys. EF shortens its native index over (Code, RoomId) on that table to the same …_C~.
  • PostgreSQL (BuildExclusionDescriptors, BuildDescriptors → ShortenConstraintCopyNames): names are made unique among the relations of the schema (indexes, keys, other constraints of the same kind) and among the constraints of the table (foreign keys, checks).
  • Copies are sorted before names are assigned, so both sides of a diff come out the same and nothing churns. Copies that share one full name share the shortened name, so the existing name checks still report two declarations that resolve alike. ValidateNamesAcrossKinds sees the shortened names, because it calls the same builders.
  • The length error, for a declaration with a TPC origin (only an explicit name gets there now), says to leave the name out, not to shorten it. That goes through an internal overload; the protected signature is unchanged. RepeatedName's advice mentions the shortening.
  • Changelogs (root, core, PostgreSQL) and the TPC section of docs/upgrading.md describe the new behaviour. TPC copies are new in the unreleased 5.5.0, so this needs no rendering-version gate.

Tests

  • TpcHierarchyTests cover:
    • a complex index (57-character table → IX_…_C~), an exclusion constraint and a temporal constraint (51-character table → EX_…_RoomId_~, AK_…_RoomId_~), each scaffolding;
    • the 64-byte case cut by exactly one character;
    • ~/~1 for two copies sharing a 62-byte prefix, for copies on two long sibling tables (unique per schema), and against EF's own shortened native name;
    • no churn on a self-diff;
    • the explicit-too-long advice on a TPC type;
    • a non-TPC long default name still refused (over-correction guard, passes either way).
  • SnapshotRoundTripTests.Tpc_shortened_copy_names_roundtrip_is_noop: a Roslyn-compiled snapshot against the live model gives no operations.
  • PostgresIntegrationTests.Tpc_long_derived_table_applies_shortened_names: on PostgreSQL 18 the names in pg_class are the scaffolded ones, unchanged, and the 23505/23P01 violations report them as ConstraintName.
  • verify-the-guard: with both differ files reverted to main, 10 of the 11 new tests fail. Each throws the ThrowIfIdentifierTooLong refusal (… is 65 bytes long, but the provider allows at most 63), except the advice test, which fails on the missing "Leave the name out". The over-correction guard passes either way, as designed.
  • Unit suite (TestCategory!=Integration&TestCategory!=SqlServer): 578/578. PostgreSQL 18 + 16 integration: 26/26.
  • migration-safety-review run over the diff: names are rendered at design time, no new operations or ordering, the round trip is covered. The one gap it raised, sibling tables that agree past the cut, now has a test (above).

🤖 Generated with Claude Code

On a TPC type with derived tables, a copy's default name that runs past the
provider's identifier limit is shortened as EF Core 10's Uniquifier shortens
its own: cut and marked '~', then '~1', '~2', ... against the names already
taken in the scope (this package's names, EF's indexes and keys, and for
constraints the table's foreign keys and checks). Measured as the provider
measures, so on PostgreSQL it cuts to 63 bytes at a whole character.

Complex indexes, exclusion constraints and temporal constraints. Every other
default name keeps the 5.2.0 refusal; an explicit name too long on a TPC
type is told to leave the name out instead of to shorten it, since a shorter
explicit name would repeat on every table.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working 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 labels Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added the area/migrations The differs, snapshot format, EnsureCreated and design-time wiring label Oct 20, 2026
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 area/migrations The differs, snapshot format, EnsureCreated and design-time wiring 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL TPC with a long derived table name: the default name is too long and an explicit name repeats, so no name passes

1 participant