Skip to content

fix(metadata-builder): keep the STI discriminator index on entities with embeddeds - #12841

Merged
michaelbromley merged 1 commit into
typeorm:masterfrom
brmk:fix/sti-discriminator-index-with-embedded
Sep 9, 2026
Merged

michaelbromley merged 1 commit into
typeorm:masterfrom
brmk:fix/sti-discriminator-index-with-embedded

Conversation

@brmk

@brmk brmk commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

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() derives indices from ownIndices:

entityMetadata.indices = entityMetadata.embeddeds.reduce(
    (indices, embedded) => indices.concat(embedded.indicesFromTree),
    entityMetadata.ownIndices,
)

With no embeddeds, reduce returns the initial value by reference, so indices is ownIndices and the later indices.push(...) in createKeysForTableInheritance() also lands in ownIndices. With one or more embeddeds, concat returns a new array, so the pushed index lives only on that detached copy. build() then re-runs computeEntityMetadataStep2() for every entity that has a generated column, which reassigns indices from ownIndices and 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 pushes the auto-created index onto ownIndices and then calls computeEntityMetadataStep2() to rebuild indices from it — the same push-and-recompute pattern EntityMetadataBuilder already uses for the indices it creates for relations. ownIndices is what every computeEntityMetadataStep2() run preserves, and the recompute happens before indices are built.

Behavior that is deliberately unchanged:

  • the existing isDiscriminatorColumnAlreadyIndexed guard 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;
  • entities without embeddeds keep exactly the one index they have today.

The same code shape exists on the v0 branch, so 0.3.x is affected as well. This PR targets master; happy to open the equivalent backport if you want it there.

Verification. Ran locally against better-sqlite3 (ormconfig.json limited to that driver): the full suite passes, and the new test file fails on master without 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) minus spanner, and I did not run the non-sqlite drivers locally.

New tests in test/functional/table-inheritance/single-table/discriminator-index-with-embedded:

  • STI base with an embedded column and a generated id — the discriminator index exists, both in EntityMetadata.indices and on the created table;
  • same shape plus a user-defined @Index() on the discriminator column — exactly one index, keeping the user's name (dedup guard intact);
  • STI base without embedded columns — still exactly one index (no regression).

Closes #12840

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword:
    Fixes #NNNN, Closes #NNNN, or Resolves #NNNN
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md) — N/A, this restores documented/intended behavior and changes no public API

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (1) 📜 Skill insights (0)

Grey Divider


Action required

1. #12840 bypasses functional tests 📘 Rule violation ⚙ Maintainability
Description
The regression coverage is added exclusively under test/github-issues/12840, despite an
established functional suite for single-table inheritance and no stated reason requiring per-issue
placement. This directly violates the requirement to keep issue fixes in the functional test suite.
Code

test/github-issues/12840/issue-12840.test.ts[31]

+describe("github issues > #12840 discriminator index is dropped when the STI entity has an embedded column", () => {
Evidence
PR Compliance ID 3 requires issue-fix coverage under test/functional and permits
test/github-issues-only coverage only with a clear reason. The new suite identifies itself as the
#12840 regression test and is located exclusively under test/github-issues/12840, while the
repository has an applicable test/functional/table-inheritance/single-table suite.

Rule 3: Prefer functional tests over per-issue tests
test/github-issues/12840/issue-12840.test.ts[31-31]
test/functional/table-inheritance/single-table/basic-functionality/basic-functionality.test.ts[1-1]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The #12840 regression tests were added only under `test/github-issues`, contrary to the requirement that issue fixes use the functional test suite unless there is a clear reason not to.
## Issue Context
Single-table inheritance already has dedicated coverage under `test/functional/table-inheritance/single-table`. Move this test and its fixtures into an appropriate functional-test directory while retaining the #12840 reference in the test description or a concise comment.
## Fix Focus Areas
- test/github-issues/12840/issue-12840.test.ts[1-101]
- test/github-issues/12840/entity/EmbeddedBase.ts[1-28]
- test/github-issues/12840/entity/IndexedBase.ts[1-33]
- test/github-issues/12840/entity/Meta.ts[1-6]
- test/github-issues/12840/entity/PlainBase.ts[1-23]
- test/github-issues/12840/entity/index.ts[1-5]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Context sources
Review mode: ⚖️ Balanced: This is a localized but behavior-changing metadata fix affecting STI schema/index generation across multiple database drivers, so it warrants a careful single-pass review.

Grey Divider

Tip of the day
💡 Did you know, you can tweak Display preferences with a live preview to see your comment before it ships

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit cd5d8a7 ⚖️ Balanced

Results up to commit 61b02c7


🐞 Bugs (0) 📘 Rule violations (1) 📜 Skill insights (0)


Action required
1. #12840 bypasses functional tests 📘 Rule violation ⚙ Maintainability
Description
The regression coverage is added exclusively under test/github-issues/12840, despite an
established functional suite for single-table inheritance and no stated reason requiring per-issue
placement. This directly violates the requirement to keep issue fixes in the functional test suite.
Code

test/github-issues/12840/issue-12840.test.ts[31]

+describe("github issues > #12840 discriminator index is dropped when the STI entity has an embedded column", () => {
Evidence
PR Compliance ID 3 requires issue-fix coverage under test/functional and permits
test/github-issues-only coverage only with a clear reason. The new suite identifies itself as the
#12840 regression test and is located exclusively under test/github-issues/12840, while the
repository has an applicable test/functional/table-inheritance/single-table suite.

Rule 3: Prefer functional tests over per-issue tests
test/github-issues/12840/issue-12840.test.ts[31-31]
test/functional/table-inheritance/single-table/basic-functionality/basic-functionality.test.ts[1-1]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The #12840 regression tests were added only under `test/github-issues`, contrary to the requirement that issue fixes use the functional test suite unless there is a clear reason not to.
## Issue Context
Single-table inheritance already has dedicated coverage under `test/functional/table-inheritance/single-table`. Move this test and its fixtures into an appropriate functional-test directory while retaining the #12840 reference in the test description or a concise comment.
## Fix Focus Areas
- test/github-issues/12840/issue-12840.test.ts[1-101]
- test/github-issues/12840/entity/EmbeddedBase.ts[1-28]
- test/github-issues/12840/entity/IndexedBase.ts[1-33]
- test/github-issues/12840/entity/Meta.ts[1-6]
- test/github-issues/12840/entity/PlainBase.ts[1-23]
- test/github-issues/12840/entity/index.ts[1-5]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Results up to commit e37aa93


🐞 Bugs (0) 📘 Rule violations (1) 📜 Skill insights (0)


Action required
1. #12840 bypasses functional tests 📘 Rule violation ⚙ Maintainability
Description
The regression coverage is added exclusively under test/github-issues/12840, despite an
established functional suite for single-table inheritance and no stated reason requiring per-issue
placement. This directly violates the requirement to keep issue fixes in the functional test suite.
Code

test/github-issues/12840/issue-12840.test.ts[31]

+describe("github issues > #12840 discriminator index is dropped when the STI entity has an embedded column", () => {
Evidence
PR Compliance ID 3 requires issue-fix coverage under test/functional and permits
test/github-issues-only coverage only with a clear reason. The new suite identifies itself as the
#12840 regression test and is located exclusively under test/github-issues/12840, while the
repository has an applicable test/functional/table-inheritance/single-table suite.

Rule 3: Prefer functional tests over per-issue tests
test/github-issues/12840/issue-12840.test.ts[31-31]
test/functional/table-inheritance/single-table/basic-functionality/basic-functionality.test.ts[1-1]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The #12840 regression tests were added only under `test/github-issues`, contrary to the requirement that issue fixes use the functional test suite unless there is a clear reason not to.

## Issue Context
Single-table inheritance already has dedicated coverage under `test/functional/table-inheritance/single-table`. Move this test and its fixtures into an appropriate functional-test directory while retaining the #12840 reference in the test description or a concise comment.

## Fix Focus Areas
- test/github-issues/12840/issue-12840.test.ts[1-101]
- test/github-issues/12840/entity/EmbeddedBase.ts[1-28]
- test/github-issues/12840/entity/IndexedBase.ts[1-33]
- test/github-issues/12840/entity/Meta.ts[1-6]
- test/github-issues/12840/entity/PlainBase.ts[1-23]
- test/github-issues/12840/entity/index.ts[1-5]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

@brmk
brmk force-pushed the fix/sti-discriminator-index-with-embedded branch from e37aa93 to 61b02c7 Compare September 5, 2026 20:59
@brmk

brmk commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

Moved the regression tests into test/functional/table-inheritance/single-table/discriminator-index-with-embedded, keeping the #12840 reference above the describe. test/github-issues/12840 is gone; the three cases are unchanged.

@pkg-pr-new

pkg-pr-new Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12841
    

commit: cd5d8a7

@michaelbromley michaelbromley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — the diagnosis is right and the write-up made this easy to follow. I reproduced it locally: the new test fails on the parent commit with expected [] to have a length of 1 and passes with the change. The rest of the sqlite suite is unaffected.

A few changes before this goes in.

Use the existing push-and-recompute pattern

The current fix adds a second writer to entityMetadata.indices:

entityMetadata.indices = entityMetadata.indices.concat(index)
entityMetadata.ownIndices.push(index)

indices is derived from ownIndices. This bug exists because the old code assigned to indices directly, and after this change two places still assign to it. createKeysForTableInheritance is the only assignment to indices in src/ outside computeEntityMetadataStep2.

EntityMetadataBuilder already handles this case three times, at lines 244-248, 254-259 and 277-281:

entityMetadata.ownIndices.push(index)
this.computeEntityMetadataStep2(entityMetadata)

Push to ownIndices, then recompute indices. The recompute runs before the index.build() loop at line 366, so the new index is still built. I applied this to createKeysForTableInheritance and ran the sqlite suite: same result, your three tests green.

This also lets you drop the nine-line comment. One line covers it:

// computeEntityMetadataStep2 rebuilds "indices" from "ownIndices".

The second half of the current comment describes how computeEntityMetadataStep2 builds indices.

PR description

The description says the tests are in test/github-issues/12840. They are in test/functional/table-inheritance/single-table/discriminator-index-with-embedded/. The functional directory is the right home; please fix the path in the description.

…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 and recompute with computeEntityMetadataStep2 - the
same push-and-recompute pattern the builder already uses for the indices it creates
for relations. The recomputation happens before indices are built, and 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
brmk force-pushed the fix/sti-discriminator-index-with-embedded branch from 61b02c7 to cd5d8a7 Compare September 8, 2026 07:31
@brmk

brmk commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Applied the push-and-recompute pattern in cd5d8a7 and reduced the comment to the one line. The description now points at test/functional/table-inheritance/single-table/discriminator-index-with-embedded instead of the wrong test/github-issues/12840 path.

@brmk
brmk requested a review from michaelbromley September 8, 2026 10:25

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @brmk

@michaelbromley
michaelbromley merged commit 7a9009d into typeorm:master Sep 9, 2026
49 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linked-issue PR references an issue

Development

Successfully merging this pull request may close these issues.

Auto-created STI discriminator index is silently dropped when the entity has an embedded column

3 participants