{{ message }}
fix(metadata-builder): keep the STI discriminator index on entities with embeddeds - #12841
Open
brmk wants to merge 1 commit into
Open
fix(metadata-builder): keep the STI discriminator index on entities with embeddeds#12841brmk wants to merge 1 commit into
brmk wants to merge 1 commit into
Conversation
…ith embeddeds createKeysForTableInheritance pushed the auto-created discriminator index onto entityMetadata.indices. That array is derived from ownIndices and is recomputed by computeEntityMetadataStep2, which build() runs again for every entity that has a generated column, so the index was discarded. It only survived when the entity had no embeddeds, because reduce then returns ownIndices by reference and the push mutated ownIndices as well. Register the index in ownIndices instead, so it survives the recomputation. Since indices can be the very same array as ownIndices, it is rebuilt with concat rather than pushed to, to avoid registering the index twice. The existing guard against indexing an already indexed discriminator column is unaffected. Regression coverage lives in test/functional/table-inheritance/single-table/discriminator-index-with-embedded. Closes typeorm#12840
brmk
force-pushed
the
fix/sti-discriminator-index-with-embedded
branch
from
September 5, 2026 20:59
e37aa93 to
61b02c7
Compare
Author
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.

Description of change
The index that
EntityMetadataBuilder.createKeysForTableInheritance()creates on an STI discriminator column is silently dropped when the entity has at least one embedded column and at least one generated column.computeEntityMetadataStep2()derivesindicesfromownIndices:With no embeddeds,
reducereturns the initial value by reference, soindicesisownIndicesand the laterindices.push(...)increateKeysForTableInheritance()also lands inownIndices. With one or more embeddeds,concatreturns a new array, so the pushed index lives only on that detached copy.build()then re-runscomputeEntityMetadataStep2()for every entity that has a generated column, which reassignsindicesfromownIndicesand discards the auto-created index. No error, no warning — the table is simply created without the index.All three conditions are needed: STI base, at least one embedded column, at least one generated column. A
@PrimaryGeneratedColumn()id supplies the third, so in practice any framework that puts an embedded column on its base entity loses every STI discriminator index (see the issue for the Vendure case and its production impact).The fix registers the auto-created index in
ownIndices, which is what everycomputeEntityMetadataStep2()run preserves. Becauseindicesmay be the very same array asownIndices, it is rebuilt withconcatrather than pushed to, so the index is never registered twice.Behavior that is deliberately unchanged:
isDiscriminatorColumnAlreadyIndexedguard still short-circuits when the user has their own@Index()on the discriminator column, so no duplicate index appears for projects that already worked around this by hand;The same code shape exists on the
v0branch, so 0.3.x is affected as well. This PR targetsmaster; happy to open the equivalent backport if you want it there.Verification. Ran locally against
better-sqlite3(ormconfig.jsonlimited to that driver): the full suite passes, and the new test file fails onmasterwithout the source change (expected [] to have a length of 1) and passes with it. The behavior is decided at the metadata layer, before any driver is involved, so the outcome is driver-independent; the new test enables the same driver list as the closest existing test (test/github-issues/10496) minusspanner, and I did not run the non-sqlite drivers locally.New tests in
test/github-issues/12840:EntityMetadata.indicesand on the created table;@Index()on the discriminator column — exactly one index, keeping the user's name (dedup guard intact);Closes #12840
Pull-Request Checklist
masterbranchFixes #NNNN,Closes #NNNN, orResolves #NNNNtests/**.test.ts)docs/docs/**.md) — N/A, this restores documented/intended behavior and changes no public API