Skip to content

fix(persistence): preserve select false columns on the in-memory entity after save() - #12501

Merged
gioboa merged 5 commits into
typeorm:masterfrom
tada5hi:fix/12473-preserve-select-false-after-save
Jun 3, 2026
Merged

gioboa merged 5 commits into
typeorm:masterfrom
tada5hi:fix/12473-preserve-select-false-after-save

Conversation

@tada5hi

@tada5hi tada5hi commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Description of change

SubjectExecutor.updateSpecialColumnsInInsertedAndUpdatedEntities deletes every select: false column from subject.entity after 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():

  1. The entity returned by save() no longer carries the value the caller just supplied.
  2. The original entity reference passed to 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 because getInsertionReturningColumns() includes them in RETURNING and subject.generatedMap is merged back into the entity after the delete. Columns without a default are stripped permanently. Same select: false decorator option, opposite outcomes.

Reproduction (no subscriber needed)

@Entity()
class User {
    @PrimaryGeneratedColumn("uuid")
    id: string

    @Column()
    name: string

    @Column({ select: false })
    email: string
}

const entity = repo.create({ name: "foo", email: "[email protected]" })
const saved = await repo.save(entity)

console.log(saved.email)        // undefined in 0.3.29, "[email protected]" in 0.3.28
console.log("email" in entity)  // false in 0.3.29, true in 0.3.28

Why revert rather than narrow the delete

The 0.3.28 contract was "select: false controls 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 use addSelect. 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: false means hidden everywhere, including post-save" semantics that #11944 introduced, that's a defensible position too — but it's a breaking change that warrants a 0.4.0 rather than a 0.3.x patch, plus a documented migration path (e.g. a reload: true option on save() that re-fetches with addSelect, or RETURNING of select: false columns when the caller explicitly opts in).

Relationship to #12493

#12493 addresses the same root cause (the same delete in SubjectExecutor) by snapshot-restoring event.entity for subscriber broadcast only. That fixes the symptom in afterInsert / afterUpdate events but leaves the user-facing repo.save() return mutated. By removing the delete entirely, this PR fixes both symptoms and makes the snapshot/restore step in #12493 unnecessary.

Tests

  • The two assertions added to test/functional/columns/select-control/select-control.test.ts in fix: prevent columns with select false from being returned #11944 are inverted to match the restored contract: select: false columns and embedded select: false columns are now expected to remain on the returned entity, and the input reference is asserted to be unmutated.
  • New test/github-issues/12473/ covers the returned entity, the input reference, and both afterInsert and afterUpdate subscribers.

Closes #12473.
Supersedes #12493.

Pull-Request Checklist

…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
Copilot AI review requested due to automatic review settings May 15, 2026 07:52
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented May 15, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0)

Grey Divider


Remediation recommended

1. Unsafe subscriber cast 🐞 Bug ☼ Reliability
Description
issue-12473.test.ts casts the result of dataSource.subscribers.find(...) to MyEntitySubscriber
without checking for undefined, so a missing subscriber will crash the test with a TypeError
before any assertions run. This can happen if directory-based subscriber loading doesn’t instantiate
the exact class reference used by instanceof (or if the subscriber isn’t registered for a given
driver).
Code

test/github-issues/12473/issue-12473.test.ts[R61-65]

Evidence
The new test uses an unchecked type assertion on the find() result and then dereferences it; a
missing subscriber will therefore throw at runtime. A comparable test shows the intended defensive
pattern (explicitly throwing when the subscriber is not found).

test/github-issues/12473/issue-12473.test.ts[57-66]
test/github-issues/8221/issue-8221.test.ts[47-51]

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 test assumes `dataSource.subscribers.find(...)` always returns a `MyEntitySubscriber` and immediately writes to `subscriber.lastAfterInsertEntity`. If `find()` returns `undefined`, the test fails with a non-actionable runtime error instead of a clear assertion/diagnostic.
### Issue Context
Other tests that access subscribers defensively check the result and throw a clear error when the subscriber is not found.
### Fix Focus Areas
- test/github-issues/12473/issue-12473.test.ts[57-106]
### Suggested fix
1. After the `find(...)`, add an explicit guard:
 - `if (!subscriber) throw new Error("Subscriber not found")`
 - or `expect(subscriber, "...").to.not.be.undefined` and then use `subscriber!`.
2. (Optional hardening) Pass the subscriber class directly to `createTestingConnections({ subscribers: [MyEntitySubscriber], ... })` to avoid any class-identity mismatch when using `instanceof`.

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


2. Docs outdated on select:false ✓ Resolved 📘 Rule violation ⚙ Maintainability
Description
This PR changes persistence behavior so save() preserves user-supplied values for select: false
columns, but the docs/release notes still state those columns are "no longer returned." This can
mislead users relying on the documented behavior.
Code

test/functional/columns/select-control/select-control.test.ts[R90-93]

Evidence
The updated functional test asserts that save() preserves the user-supplied select: false value
(authorName) on both the returned entity and the original reference, demonstrating a user-facing
behavior change. However, the release notes still claim columns with select: false are "no longer
returned", which is now inconsistent with the restored save() behavior.

Rule 2: Docs updated for user-facing changes
test/functional/columns/select-control/select-control.test.ts[90-104]
src/persistence/SubjectExecutor.ts[1043-1052]
docs/docs/releases/1.0/01-release-notes.md[132-140]

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 PR changes user-visible persistence behavior: `save()` now preserves user-supplied values for `select: false` columns on the returned entity (and does not mutate the input reference), but documentation still implies `select: false` columns are not returned.
## Issue Context
- Tests were updated to assert `save()` preserves `select: false` values.
- Release notes currently state: "Columns with `select: false` no longer returned", which may now be inaccurate/misleading given the restored behavior.
## Fix Focus Areas
- docs/docs/releases/1.0/01-release-notes.md[132-140]
- docs/docs/query-builder/1-select-query-builder.md[1165-1199]

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


Grey Divider

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

Qodo Logo

`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.
@pkg-pr-new

pkg-pr-new Bot commented May 15, 2026 •

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

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

commit: 7fc201b

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@tada5hi tada5hi changed the title fix(persistence): preserve select: false columns on the in-memory entity after save() fix: preserve select: false columns on the in-memory entity after save() May 15, 2026
@tada5hi tada5hi changed the title fix: preserve select: false columns on the in-memory entity after save() fix(persistence): preserve select false columns on the in-memory entity after save() May 15, 2026
@tada5hi

tada5hi commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

@alumni

@alumni
alumni requested review from alumni and gioboa May 19, 2026 13:52

@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 @tada5hi for your help

@gioboa
gioboa merged commit 324c46c into typeorm:master Jun 3, 2026
46 checks passed
@github-actions github-actions Bot added this to the 1.0 milestone Jun 3, 2026
@alumni alumni added the backport This fix should be backported to a previous version label Jun 3, 2026
@alumni

alumni commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

@tada5hi thanks for the PR and sorry for the slow reply - yes, select: false should not remove the property after saving - for the original issue we might introduce a separate option in the future if requested by users, as mentioned here #4591 (comment)

alumni added a commit that referenced this pull request Jun 4, 2026
…ty after save() (#12501)

(cherry picked from commit 324c46c)

Co-authored-by: Lucian Mocanu <[email protected]>
@alumni alumni removed the backport This fix should be backported to a previous version label Jun 12, 2026
@alumni

alumni commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Removed the "backport" label because it was already backported - the fix will be available in both 0.3 and 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

select: false columns are no longer available in event subscriber afterUpdate

4 participants