Skip to content

Let a custom foreign key name survive conjoined tenancy, and shorten the generated ones - #5678

Merged
jeremydmiller merged 1 commit into
masterfrom
fix/5672-custom-fk-name-conjoined
Oct 15, 2026
Merged

jeremydmiller merged 1 commit into
masterfrom
fix/5672-custom-fk-name-conjoined

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Closes #5672.

The bug as reported

Under conjoined tenancy, DocumentTable moves tenant_id to the front of every document foreign key and, while doing it, unconditionally regenerated the key's name. A name set through the foreignKeyConfiguration callback was therefore thrown away.

That is what made this unfixable rather than merely annoying. The failure the reporter hit is an identifier-length error whose message says "You may want to change this value … or by explicitly overriding the database object name that is violating the length rule" — and overriding the name had no effect, because this method overwrote it a moment later.

The fix

TryMoveTenantIdFirst now regenerates the name only when it is still the one DocumentForeignKey generated, tracked on the key itself (read back after construction, since the base constructor unquotes it). The column and linked-name reordering, which is the reason the method exists, is unchanged in every case.

A plain ForeignKey — the schemaName/tableName overload, or one added by hand — is now left alone too. Its name was never Marten's to generate, so nothing here can tell a default from a deliberate choice.

Both generated names also go through PostgresqlIdentifier.Shorten, the same treatment the document table's own primary key got in #5271/#4679, so the escape hatch is no longer the only way through. It is a no-op at 63 characters or fewer, and appends a deterministic hash rather than truncating, so two long type names that agree for the first 58 characters do not collide into one constraint.

That is option 1 and option 2 from the issue. Option 3 (a columnName parameter on Duplicate/ForeignKey) is a separate API addition and is not here.

The index, which the issue does not mention

Fixing the foreign key alone does not fix the reported scenario. Schema.For<T>().ForeignKey<TReference>(...) adds a DocumentIndex beside the key, and its generated name goes over the limit on the same line of configuration:

mt_doc_verylongdocumentnamedetails_idx_very_long_reference_document_id    (70 characters)

So with only the key fixed, startup still failed — one identifier further on. The new tests caught exactly that, which is why DocumentIndex.deriveIndexName is in this PR. It is the same one-line Shorten, with the same no-op-under-the-limit property.

Why the suite never saw it

OneOffConfigurationsContext raises NameDataLength to 100, so the generated 81-character name fit. Two of the new tests pin it to PostgreSQL's own default (64) and apply the schema for real against the database.

Tests

Bug_5672_custom_foreign_key_name_under_conjoined_tenancy, 6 tests:

a_custom_name_survives_the_tenant_id_reordering the name is kept and tenant_id is still moved to the front
the_schema_applies_at_the_real_identifier_limit_with_a_custom_name NameDataLength = 64, apply + AssertDatabaseMatchesConfigurationAsync
the_schema_applies_at_the_real_identifier_limit_with_the_generated_name the same without any custom name — shortening alone is enough
the_generated_name_is_still_regenerated_with_tenant_id_first the reordering's own behaviour is unchanged for a generated name
a_generated_name_over_the_identifier_limit_is_shortened_rather_than_refused and matches Shorten of the full name
single_tenancy_is_untouched no reordering, custom name kept

A/B against the unfixed code: 4 of the 6 fail (the two that pass are the behaviour-preservation tests, which should pass both ways). With the fix: 6/6.

Full DocumentDbTests on net10.0: 1145 passed, 1 skipped, 0 failed — nothing in the suite depended on either generated name.

One thing to weigh

Shorten's default is PostgreSQL's 63, not the configured NameDataLength, matching the existing call in DocumentTable and EventTagTable. So an application that raised NAMEDATALEN and has a foreign key or index name between 64 and its own limit will see that one object renamed, and a migration diff for it. That trade is already taken for the primary key name; I kept it consistent rather than threading the configured length through only here. Say the word if you would rather it respected NameDataLength.

🤖 Generated with Claude Code

https://claude.ai/code/session_01481eiEZg1Bv6pu4DrgPRg3

…shorten the generated ones

Under conjoined tenancy, DocumentTable moves tenant_id to the front of every document foreign key
and regenerated the key's name while doing it, unconditionally. A name set through the
foreignKeyConfiguration callback was therefore thrown away -- which is what made the reported failure
unfixable rather than merely annoying: the identifier-length exception tells you to override the
database object name, and overriding it had no effect.

TryMoveTenantIdFirst now regenerates the name only when it is still the one DocumentForeignKey
generated, tracked on the key itself. A plain ForeignKey -- the schemaName/tableName overload, or one
added by hand -- is left alone too; its name was never Marten's to generate, so nothing here can tell
a default from a choice. The column and linked-name reordering, which is why the method exists, is
unchanged in every case.

Both generated names are also run through PostgresqlIdentifier.Shorten, the same treatment the
document table's own primary key got in #5271/#4679, so the escape hatch is no longer the only way
through. It is a no-op at 63 characters or fewer and appends a deterministic hash rather than
truncating, so two long type names that agree for the first 58 characters do not collide.

That covers the index as well as the key. Schema.For<T>().ForeignKey<TReference>(...) adds a
DocumentIndex beside the foreign key, and its generated name goes over the limit on the same line of
configuration: fixing only the key left the reported failure in place one identifier further on
(mt_doc_verylongdocumentnamedetails_idx_very_long_reference_document_id is 70 characters). The new
tests caught that, which is why DocumentIndex.deriveIndexName is in this commit.

The suite never saw any of this because the test harness raises NameDataLength to 100, so the
generated 81 character name fit. Two of the new tests pin it to PostgreSQL's own default and apply
the schema for real.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01481eiEZg1Bv6pu4DrgPRg3
@jeremydmiller

Copy link
Copy Markdown
Member Author

Also ran CoreTests on net10.0 for the identifier-name changes (it holds the ForeignKeys and Partitioning suites): 818 passed, 8 skipped, 0 failed.

@jeremydmiller
jeremydmiller merged commit df92733 into master Oct 15, 2026
31 checks passed
@jeremydmiller
jeremydmiller deleted the fix/5672-custom-fk-name-conjoined branch October 15, 2026 19:18
This was referenced Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom ForeignKey.Name is overwritten under conjoined tenancy, so overlong foreign key names can't be fixed

1 participant