Repository navigation
fix(metadata-builder): keep the STI discriminator index on entities with embeddeds - #12841
Conversation
Code Review by Qodo
1. #12840 bypasses functional tests
|
e37aa93 to
61b02c7
Compare
|
Moved the regression tests into |
commit: |
michaelbromley
left a comment
There was a problem hiding this comment.
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
61b02c7 to
cd5d8a7
Compare
|
Applied the push-and-recompute pattern in cd5d8a7 and reduced the comment to the one line. The description now points at |
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 pushes the auto-created index onto
ownIndicesand then callscomputeEntityMetadataStep2()to rebuildindicesfrom it — the same push-and-recompute patternEntityMetadataBuilderalready uses for the indices it creates for relations.ownIndicesis what everycomputeEntityMetadataStep2()run preserves, and the recompute happens before indices are built.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/functional/table-inheritance/single-table/discriminator-index-with-embedded: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