Repository navigation
Let a custom foreign key name survive conjoined tenancy, and shorten the generated ones - #5678
Merged
Merged
Conversation
…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
Member
Author
|
Also ran |
This was referenced Oct 16, 2026
This was referenced Oct 23, 2026
This was referenced Nov 12, 2026
Closed
This was referenced Nov 20, 2026
This was referenced Nov 27, 2026
This was referenced Dec 4, 2026
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 #5672.
The bug as reported
Under conjoined tenancy,
DocumentTablemovestenant_idto the front of every document foreign key and, while doing it, unconditionally regenerated the key's name. A name set through theforeignKeyConfigurationcallback 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
TryMoveTenantIdFirstnow regenerates the name only when it is still the oneDocumentForeignKeygenerated, 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— theschemaName/tableNameoverload, 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
columnNameparameter onDuplicate/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 aDocumentIndexbeside the key, and its generated name goes over the limit on the same line of configuration:So with only the key fixed, startup still failed — one identifier further on. The new tests caught exactly that, which is why
DocumentIndex.deriveIndexNameis in this PR. It is the same one-lineShorten, with the same no-op-under-the-limit property.Why the suite never saw it
OneOffConfigurationsContextraisesNameDataLengthto 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_reorderingtenant_idis still moved to the frontthe_schema_applies_at_the_real_identifier_limit_with_a_custom_nameNameDataLength = 64, apply +AssertDatabaseMatchesConfigurationAsyncthe_schema_applies_at_the_real_identifier_limit_with_the_generated_namethe_generated_name_is_still_regenerated_with_tenant_id_firsta_generated_name_over_the_identifier_limit_is_shortened_rather_than_refusedShortenof the full namesingle_tenancy_is_untouchedA/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
DocumentDbTestson 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 configuredNameDataLength, matching the existing call inDocumentTableandEventTagTable. So an application that raisedNAMEDATALENand 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 respectedNameDataLength.🤖 Generated with Claude Code
https://claude.ai/code/session_01481eiEZg1Bv6pu4DrgPRg3