Skip to content

fix: prevent columns with select false from being returned - #11944

Merged
gioboa merged 2 commits into
typeorm:masterfrom
gioboa:fix/4591
Jan 27, 2026
Merged

gioboa merged 2 commits into
typeorm:masterfrom
gioboa:fix/4591

Conversation

@gioboa

@gioboa gioboa commented Jan 25, 2026 •

Copy link
Copy Markdown
Collaborator

Description of change

This change is correctly handling the select property defined in columns.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links relevant issues as Close .save() returns columns marked select: false #4591
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

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

Copy link
Copy Markdown

User description

Description of change

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links relevant issues as Close #4591
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

PR Type

Bug fix, Tests


Description

  • Prevent columns with select: false from being returned in save operations

  • Delete excluded columns from entity after insert/update operations

  • Add test coverage for excluded columns in regular and embedded entities

  • Add JSDoc parameter documentation to methods


Diagram Walkthrough

flowchart LR
  A["Save Operation"] --> B["updateSpecialColumnsInInsertedAndUpdatedEntities"]
  B --> C["Check column.isSelect"]
  C --> D["Delete from entity if false"]
  D --> E["Cleaned Entity Returned"]
Loading

File Walkthrough

Relevant files
Bug fix
SubjectExecutor.ts
Remove select: false columns from saved entities                 

src/persistence/SubjectExecutor.ts

  • Added logic to delete columns marked with select: false from entities
    after insert/update
  • Handles both regular columns and embedded entity columns using
    OrmUtils.deepValue
  • Added JSDoc @param documentation for
    updateSpecialColumnsInInsertedAndUpdatedEntities and groupBulkSubjects
    methods
+16/-0   
Tests
columns-no-select.test.ts
Add tests for select: false column exclusion                         

test/functional/columns/no-select/columns-no-select.test.ts

  • Added test to verify columns with select: false are not returned after
    save
  • Added test to verify excluded columns in embedded entities are not
    returned
  • Both tests validate that excluded columns are properly removed from
    saved entity results
+35/-0   
Post.ts
Add Counters embedded entity for testing                                 

test/functional/columns/no-select/entity/Post.ts

  • Created Counters class with secretCode column marked as select: false
  • Added counters embedded column to Post entity for testing embedded
    entity exclusion
+8/-0     

@pkg-pr-new

pkg-pr-new Bot commented Jan 25, 2026 •

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

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

commit: 02881bc

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

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
High-level
Avoid mutating the original entity

Instead of mutating the original entity by deleting properties with select:
false, the .save() method should return a cloned object with these properties
removed. This avoids unexpected side effects and preserves the integrity of the
original entity object.

Examples:

src/persistence/SubjectExecutor.ts [1088-1099]
                if (!column.isSelect) {
                    const target = column.embeddedMetadata
                        ? OrmUtils.deepValue(
                              subject.entity!,
                              column.embeddedMetadata.propertyPath,
                          )
                        : subject.entity

                    if (target) {
                        delete target[column.propertyName]

 ... (clipped 2 lines)

Solution Walkthrough:

Before:

// In SubjectExecutor.ts
updateSpecialColumnsInInsertedAndUpdatedEntities(subjects: Subject[]): void {
    subjects.forEach((subject) => {
        // ...
        subject.metadata.columns.forEach((column) => {
            if (!column.isSelect) {
                // `subject.entity` is the original user-provided object
                const target = column.embeddedMetadata
                    ? OrmUtils.deepValue(subject.entity!, ...)
                    : subject.entity;
                if (target) {
                    delete target[column.propertyName]; // Mutates the original object
                }
            }
        });
    });
}

After:

// The core idea is to not modify subject.entity directly.
// Instead, the returned object from `.save()` should be a separate, modified instance.
// This could be implemented by cloning the entity before returning it from the `save` call,
// and applying the property deletion to the clone.

// Conceptual change in `save` method's return value handling:
async save<T>(entity: T): Promise<T> {
    // ... internal save logic which uses SubjectExecutor ...
    // The entity is persisted and updated with DB values.

    // After persistence, before returning:
    const entityToReturn = clone(entity); // Create a clone

    // Apply property removal to the clone, not the original
    this.removeNonSelectableProperties(entityToReturn);

    return entityToReturn;
}
Suggestion importance[1-10]: 9

__

Why: The suggestion correctly identifies a critical side effect—mutating the original entity—which is poor API design and can lead to unexpected bugs for users of the library.

High
  • More

@gioboa gioboa changed the title fix: prevent columns with select: false from being returned fix: prevent columns with select false from being returned Jan 25, 2026
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jan 25, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (2) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Missing documentation for save() behavior 📘 Rule violation ✓ Correctness
Description
• The PR changes user-facing behavior of the .save() method to exclude select: false columns
  from returned entities
• The documentation only describes select: false behavior for query operations, not for .save()
  method
• The PR checklist explicitly marks documentation as not updated despite this being a user-facing
  change
Code

src/persistence/SubjectExecutor.ts[R1088-1099]

+                if (!column.isSelect) {
+                    const target = column.embeddedMetadata
+                        ? OrmUtils.deepValue(
+                              subject.entity!,
+                              column.embeddedMetadata.propertyPath,
+                          )
+                        : subject.entity
+
+                    if (target) {
+                        delete target[column.propertyName]
+                    }
+                }
Evidence
The compliance rule requires documentation updates for user-facing changes. The PR modifies the
behavior of repository.save() to exclude columns marked with select: false from the returned
entity. The existing documentation at docs/docs/entity/1-entities.md:438 and
docs/docs/query-builder/1-select-query-builder.md:1148 only describes select: false for query
operations, not for save operations. The PR description checklist shows documentation was not
updated (unchecked box).

Rule 2: Docs updated for user-facing changes
src/persistence/SubjectExecutor.ts[1088-1099]

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 `.save()` method now excludes columns marked with `select: false` from returned entities, but this behavior is not documented.

## Issue Context
The existing documentation only describes `select: false` behavior for queries (find, QueryBuilder). Users need to know that `.save()` also respects this setting and returns entities without these columns.

## Fix Focus Areas
- docs/docs/entity/1-entities.md[438-438] - Update the `select: boolean` description to mention save() behavior
- docs/docs/working-with-entity-manager/6-repository-api.md[95-105] - Add note to save() method documentation about select: false columns

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



Remediation recommended

2. JSDoc @PARAM tags lack descriptions 📘 Rule violation ✓ Correctness
Description
• The added JSDoc comments contain only @param tags without parameter descriptions, which is
  inconsistent with TypeScript/JSDoc standards
• These bare @param tags provide no value to developers and appear to be AI-generated noise
• The existing codebase does not use this pattern - other methods either have no JSDoc or have
  complete documentation
Code

src/persistence/SubjectExecutor.ts[1056]

+     * @param subjects
Evidence
The compliance rule requires removal of AI-generated noise including extra comments that are
inconsistent with file style. The added @param subjects and @param type tags at lines 1056 and
1146-1147 have no descriptions and are inconsistent with the existing documentation style in
SubjectExecutor.ts. Other methods in the file either have complete JSDoc documentation (like lines
233-235) or no JSDoc at all, but none use bare @param tags without descriptions.

Rule 4: Remove AI-generated noise
src/persistence/SubjectExecutor.ts[1056-1056]
src/persistence/SubjectExecutor.ts[1146-1147]
src/persistence/SubjectExecutor.ts[233-235]

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 added JSDoc `@param` tags without descriptions at lines 1056, 1146, and 1147 in SubjectExecutor.ts. These provide no documentation value and are inconsistent with the file&#x27;s existing style.

## Issue Context
The existing methods in SubjectExecutor.ts either have complete JSDoc documentation or no JSDoc at all. Bare @param tags without descriptions are not used elsewhere in the file and appear to be AI-generated noise.

## Fix Focus Areas
- src/persistence/SubjectExecutor.ts[1056-1056]
- src/persistence/SubjectExecutor.ts[1146-1147]

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


3. Potential crash with undefined embedded entities 🐞 Bug ⛯ Reliability
Description
• The code attempts to delete properties from embedded entities using OrmUtils.deepValue() without
  verifying the embedded object exists
• OrmUtils.deepValue() traverses nested paths by accessing properties sequentially (line 161: `obj
  = obj[segments[i]]`), which will throw an error if any intermediate object is null/undefined
• If an embedded entity is not initialized but contains a column with select: false, the code will
  crash when trying to access the property path
Code

src/persistence/SubjectExecutor.ts[R1088-1099]

+                if (!column.isSelect) {
+                    const target = column.embeddedMetadata
+                        ? OrmUtils.deepValue(
+                              subject.entity!,
+                              column.embeddedMetadata.propertyPath,
+                          )
+                        : subject.entity
+
+                    if (target) {
+                        delete target[column.propertyName]
+                    }
+                }
Evidence
The code uses OrmUtils.deepValue() to traverse embedded entity paths, but this utility doesn't
handle null/undefined intermediate objects. When an embedded entity is not initialized (e.g.,
post.counters is undefined), attempting to access a nested property will cause a runtime error. The
test cases only cover scenarios where the embedded entity is either fully initialized or not set at
all, missing the edge case where the entity exists but the embedded property is undefined.

src/util/OrmUtils.ts[158-164]
src/persistence/SubjectExecutor.ts[1088-1099]
test/functional/columns/no-select/entity/Post.ts[5-8]

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 code attempts to delete properties from embedded entities without checking if the embedded object exists. When `OrmUtils.deepValue()` is called with a path to a non-existent embedded entity, it will throw a runtime error because it tries to access properties on undefined objects.

# Issue Context
In the `updateSpecialColumnsInInsertedAndUpdatedEntities` method, when processing columns with `select: false` that belong to embedded entities, the code uses `OrmUtils.deepValue()` to navigate to the embedded object. However, if the embedded entity property is undefined (e.g., `post.counters` is undefined), the deepValue function will crash.

Example scenario:
```typescript
const post = new Post()
post.title = &quot;Hello&quot;
// post.counters is undefined
// When trying to delete counters.secretCode, deepValue will crash
```

# Fix Focus Areas
- src/persistence/SubjectExecutor.ts[1088-1099]: Add null safety check before calling OrmUtils.deepValue() or wrap it in a try-catch block. Consider checking if the embedded entity exists before attempting to delete properties from it.

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



ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Previous review results

Review updated until commit 02881bc

Results up to commit 334f2d0


🐞 Bugs (1) 📘 Rule violations (1) 📎 Requirement gaps (0)

Grey Divider
Action required
1. Missing documentation for save() behavior change 📘 Rule violation ✓ Correctness
Description
• The PR changes the behavior of the .save() method to exclude columns marked with select: false
  from the returned entity object
• This is a user-facing behavior change that affects how developers interact with saved entities
• The existing documentation describes select: false behavior for queries but does not mention the
  .save() method's return value behavior
• The PR checklist item for documentation updates is marked as incomplete, indicating awareness that
  documentation should be updated
Code

src/persistence/SubjectExecutor.ts[R1088-1099]

+                if (!column.isSelect) {
+                    const target = column.embeddedMetadata
+                        ? OrmUtils.deepValue(
+                              subject.entity!,
+                              column.embeddedMetadata.propertyPath,
+                          )
+                        : subject.entity
+
+                    if (target) {
+                        delete target[column.propertyName]
+                    }
+                }
Evidence
The compliance rule requires documentation updates for user-facing changes. The PR modifies the
behavior of .save() to exclude select: false columns from returned entities. The existing
documentation at docs/docs/entity/1-entities.md:438 and docs/docs/getting-started.md:536-537
describes select: false behavior for queries and .save() return values respectively, but does
not mention this new behavior. The PR description checklist shows documentation is not updated
(unchecked box).

Rule 2: Docs updated for user-facing changes
src/persistence/SubjectExecutor.ts[1088-1099]

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 `.save()` method now excludes columns marked with `select: false` from the returned entity object. This behavior change needs to be documented so users understand that `select: false` columns are consistently excluded from both query results and save operation results.

## Issue Context
The PR fixes issue #4591 where `.save()` was returning columns marked with `select: false`, which was inconsistent with query behavior. The fix ensures these columns are deleted from the returned entity object.

## Fix Focus Areas
- docs/docs/entity/1-entities.md[438-438]
- docs/docs/getting-started.md[536-537]
- docs/docs/working-with-entity-manager/5-entity-manager-api.md[141-150]

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


2. Unsafe embedded object traversal causes runtime errors 🐞 Bug ⛯ Reliability
Description
• The code uses OrmUtils.deepValue() to traverse embedded object paths without null-safety checks,
  which will throw a runtime error if any intermediate property is undefined
• The OrmUtils.deepValue() implementation (lines 158-163) iterates through path segments using
  obj = obj[segments[i]] without checking if obj is null/undefined
• If an embedded object like counters is not initialized, accessing its path will fail with
  'Cannot read property of undefined' error
• The guard if (target) only checks the final result after traversal, not during traversal, so it
  cannot prevent the error
Code

src/persistence/SubjectExecutor.ts[R1088-1099]

+                if (!column.isSelect) {
+                    const target = column.embeddedMetadata
+                        ? OrmUtils.deepValue(
+                              subject.entity!,
+                              column.embeddedMetadata.propertyPath,
+                          )
+                        : subject.entity
+
+                    if (target) {
+                        delete target[column.propertyName]
+                    }
+                }
Evidence
The OrmUtils.deepValue implementation lacks null-safety checks during path traversal. When the code
attempts to delete properties from embedded objects that haven't been initialized, the deepValue
function will throw an error. The setEntityValue method auto-initializes embedded objects (lines
905-907 in ColumnMetadata.ts), but this new delete code path doesn't benefit from that protection
since it directly calls deepValue without going through setEntityValue.

src/util/OrmUtils.ts[158-163]
src/metadata/ColumnMetadata.ts[905-907]
src/persistence/SubjectExecutor.ts[1088-1099]

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 code attempts to delete properties from embedded objects using `OrmUtils.deepValue()` which can throw runtime errors if any intermediate property in the path is undefined. This happens because `deepValue()` doesn&#x27;t check for null/undefined during traversal.

## Issue Context
When processing columns with `select: false` in embedded entities, the code needs to safely traverse the embedded object path before deleting the property. Currently, if an embedded object hasn&#x27;t been initialized by the user, the traversal fails.

## Fix Focus Areas
- src/persistence/SubjectExecutor.ts[1088-1099]: Add safe traversal logic before attempting to delete the property

Consider one of these approaches:
1. Create a safe version of `deepValue()` that returns undefined instead of throwing on null/undefined intermediate values
2. Use optional chaining or manual null checks to traverse the path safely
3. Initialize embedded objects before attempting to delete properties (similar to how `setEntityValue` does it)

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


Qodo Logo

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 81.122% (+0.004%) from 81.118%
when pulling 334f2d0 on gioboa:fix/4591
into 710d176 on typeorm:master.

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

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

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

Copy link
Copy Markdown

Persistent review updated to latest commit 02881bc

@gioboa
gioboa merged commit 56137a1 into typeorm:master Jan 27, 2026
38 checks passed
@gioboa
gioboa deleted the fix/4591 branch January 27, 2026 08:18
kranners pushed a commit to kranners/typeorm that referenced this pull request Mar 1, 2026
@pkuczynski pkuczynski added this to the 1.0 milestone Mar 8, 2026
tada5hi added a commit to tada5hi/typeorm that referenced this pull request May 15, 2026
`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.
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.

.save() returns columns marked select: false

5 participants