Repository navigation
fix(persistence): preserve select false columns on the in-memory entity after save() - #12501
Conversation
…ity after save() `SubjectExecutor.updateSpecialColumnsInInsertedAndUpdatedEntities` deleted every `select: false` column from `subject.entity` after persistence (introduced in typeorm#11944). This had two unintended effects: 1. The entity returned by `Repository.save()` no longer carried the value the caller had just supplied. 2. The original entity reference passed to `save()` was mutated in place, so application code holding that reference also lost the field. It also produced an asymmetry: columns with a default (or `@CreateDateColumn`, `@Generated`, ...) survived because the `generatedMap` merge runs *after* the delete and re-adds the DB-side value, while columns without a default were stripped permanently. Same `select: false` decorator option, opposite outcomes depending on whether `default` was set. The 0.3.28 contract was that `select: false` controls what comes *out of the DB on a read* (callers who want it back use `addSelect`). The in-memory entity is untouched by persist. This patch restores that contract by removing the post-persist delete. The behavior added in typeorm#11944 — stripping the column from the returned entity — was a breaking semantic change that warrants a major version bump (0.4.0) and an opt-in flag rather than a 0.3.x patch. This also fixes typeorm#12473: `event.entity` in `afterInsert` / `afterUpdate` subscribers now retains `select: false` columns, because the subscriber broadcaster reads from the same `subject.entity` reference that is no longer mutated. The snapshot-restore approach proposed in typeorm#12493 becomes unnecessary. The two assertions added to `columns/select-control` in typeorm#11944 are inverted to match the restored contract; new regression tests under `test/github-issues/12473` cover the returned entity, the input reference, and both subscriber events. Fixes typeorm#12473
Code Review by Qodo
1. Unsafe subscriber cast
|
`repository.save()` preserves user-supplied values for `select: false` columns on both the returned entity and the input reference. Update the 1.0 release note for typeorm#11944 and the query-builder "Hidden Columns" section to reflect the restored contract.
commit: |
|
@tada5hi thanks for the PR and sorry for the slow reply - yes, |
…ty after save() (#12501) (cherry picked from commit 324c46c) Co-authored-by: Lucian Mocanu <[email protected]>
|
Removed the "backport" label because it was already backported - the fix will be available in both 0.3 and 1.0 |
Description of change
SubjectExecutor.updateSpecialColumnsInInsertedAndUpdatedEntitiesdeletes everyselect: falsecolumn fromsubject.entityafter persistence (introduced in #11944). This patch removes that delete and restores the 0.3.28 contract.Problem
The post-persist delete added in #11944 has two unintended effects on
Repository.save():save()no longer carries the value the caller just supplied.save()is mutated in place, so application code holding that reference also loses the field.It also produces an asymmetry: columns with
default !== undefined(or@CreateDateColumn,@Generated, ...) survive becausegetInsertionReturningColumns()includes them inRETURNINGandsubject.generatedMapis merged back into the entity after the delete. Columns without a default are stripped permanently. Sameselect: falsedecorator option, opposite outcomes.Reproduction (no subscriber needed)
Why revert rather than narrow the delete
The 0.3.28 contract was "
select: falsecontrols what comes out of the DB on a read; the in-memory entity is untouched by persist." Callers who want the value back from a subsequent fetch useaddSelect. Restoring that contract for both the returned entity and the subscriber event keeps existing user code working without a new opt-in flag, and removes the default-vs-non-default asymmetry above.If the maintainers prefer the stricter "
select: falsemeans hidden everywhere, including post-save" semantics that #11944 introduced, that's a defensible position too — but it's a breaking change that warrants a0.4.0rather than a0.3.xpatch, plus a documented migration path (e.g. areload: trueoption onsave()that re-fetches withaddSelect, orRETURNINGofselect: falsecolumns when the caller explicitly opts in).Relationship to #12493
#12493 addresses the same root cause (the same delete in
SubjectExecutor) by snapshot-restoringevent.entityfor subscriber broadcast only. That fixes the symptom inafterInsert/afterUpdateevents but leaves the user-facingrepo.save()return mutated. By removing the delete entirely, this PR fixes both symptoms and makes the snapshot/restore step in #12493 unnecessary.Tests
test/functional/columns/select-control/select-control.test.tsin fix: prevent columns with select false from being returned #11944 are inverted to match the restored contract:select: falsecolumns and embeddedselect: falsecolumns are now expected to remain on the returned entity, and the input reference is asserted to be unmutated.test/github-issues/12473/covers the returned entity, the input reference, and bothafterInsertandafterUpdatesubscribers.Closes #12473.
Supersedes #12493.
Pull-Request Checklist
select: falsecolumns are no longer available in event subscriber afterUpdate #12473