Skip to content

fix: merging into an entity now respects null values - #11154

Merged
alumni merged 6 commits into
typeorm:masterfrom
knoid:valid-null
Jan 15, 2026
Merged

alumni merged 6 commits into
typeorm:masterfrom
knoid:valid-null

Conversation

@knoid

@knoid knoid commented Nov 28, 2024 •

Copy link
Copy Markdown
Contributor

Description of change

This change fixes the .merge method of Repository and EntityManager classes and now respects null values on nullable columns. Before this change, null values would just get ignored just like undefined.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply prettier formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

@knoid
knoid force-pushed the valid-null branch 4 times, most recently from 574df65 to 493a24b Compare November 28, 2024 19:42
@knoid knoid changed the title merge with null values fix: merging into an entity now respects null values Nov 28, 2024
@knoid
knoid marked this pull request as ready for review November 28, 2024 20:18

@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 for your help @knoid
I added this PR to our release board

@gioboa gioboa added the size-s label Jan 24, 2025
@coderabbitai

coderabbitai Bot commented Sep 21, 2025

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@gioboa has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 15 minutes and 17 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between d4f7b44 and 3f18464.

📒 Files selected for processing (3)
  • src/metadata/ColumnMetadata.ts (1 hunks)
  • test/other-issues/merge-entity-with-null/entity/Post.ts (1 hunks)
  • test/other-issues/merge-entity-with-null/merge-entity-with-null.ts (1 hunks)
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Sep 21, 2025 •

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

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

commit: b233bf2

@coveralls

coveralls commented Sep 21, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 80.881% (-0.001%) from 80.882%
when pulling b233bf2 on knoid:valid-null
into 13ab7c3 on typeorm:master.

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

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

Copy link
Copy Markdown

PR Code Suggestions ✨

Latest suggestions up to b233bf2

CategorySuggestion                                                                                                                                    Impact
General
Verify database persistence of null

Enhance the test to verify that the null value is persisted to the database by
fetching the entity after saving and asserting its property is null.

test/other-issues/merge-entity-with-null/merge-entity-with-null.test.ts [25-31]

 const post = new Post()
 post.title = "Hello Post"
 
 connection.manager.merge(Post, post, { title: null })
 await connection.manager.save(post)
 
-expect(post.title).to.be.null
+const savedPost = await connection.manager.findOne(Post, { where: { id: post.id } })
+expect(savedPost?.title).to.be.null
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly points out that the test only verifies the in-memory state and proposes a crucial addition to assert against the database, making the test more robust and comprehensive.

Medium
Fix incorrect test description

Correct the describe block's title in the test file to accurately reflect that
the test is about merging entities with null values, not about 'using limit in
conjunction with order by'.

test/other-issues/merge-entity-with-null/merge-entity-with-null.test.ts [11]

-describe("other issues > using limit in conjunction with order by", () => {
+describe("other issues > merge entity with null values", () => {
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies a misleading test suite description, and fixing it improves test suite clarity and maintainability for future developers.

Low
Possible issue
Clarify null vs undefined handling

In getEntityValue, explicitly return null or undefined instead of returning the
entity parameter to improve clarity.

src/metadata/ColumnMetadata.ts [770-774]

 getEntityValue(
     entity: ObjectLiteral,
     transform: boolean = false,
 ): any | undefined {
-    if (entity === undefined || entity === null) return entity
+    if (entity === undefined) return undefined
+    if (entity === null) return null

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 2

__

Why: The suggested code is functionally identical to the existing code, offering only a minor improvement in readability by being more explicit, while the reasoning about the code being 'problematic' is an overstatement.

Low
  • More

Previous suggestions

Suggestions up to commit ba5c6a8
CategorySuggestion                                                                                                                                    Impact
General
Update return type signature

Update the getEntityValue method's return type signature to any | undefined |
null to accurately reflect that it can return null.

src/metadata/ColumnMetadata.ts [770-774]

 getEntityValue(
     entity: ObjectLiteral,
     transform: boolean = false,
-): any | undefined {
+): any | undefined | null {
     if (entity === undefined || entity === null) return entity
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies that the PR change allows the function to return null, which is not explicitly in the any | undefined return type, thus improving type safety and code clarity.

Low
Fix incorrect test description

Update the test suite's describe block to accurately reflect that it tests
merging entities with null values, not "using limit in conjunction with order
by".

test/other-issues/merge-entity-with-null/merge-entity-with-null.test.ts [11]

-describe("other issues > using limit in conjunction with order by", () => {
+describe("other issues > merging entity with null values", () => {
Suggestion importance[1-10]: 4

__

Why: The suggestion correctly points out a misleading test suite description, which is likely a copy-paste error, and proposes a more accurate description improving test clarity and maintainability.

Low
Suggestions up to commit 33d2362
CategorySuggestion                                                                                                                                    Impact
General
Fix incorrect test description

Update the test suite description to accurately reflect that it tests merging
entities with null values, not issues with limit and order by.

test/other-issues/merge-entity-with-null/merge-entity-with-null.ts [11]

-describe("other issues > using limit in conjunction with order by", () => {
+describe("other issues > merge entity with null", () => {
Suggestion importance[1-10]: 4

__

Why: The suggestion correctly identifies a misleading test suite description and proposes a more accurate one, which improves test readability and maintainability.

Low

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

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@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.

LGTM 👏 Thanks @knoid

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

Copy link
Copy Markdown

Code Review by Qodo (Alpha)

🐞 Bugs (1) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider


Advisory Comments

1. Merge Operation Now Applies Null Values 🐞 Bug
Description
• The change enables merge operations to explicitly set null values on entity properties, altering
  the previous behavior where null values were skipped
• In PlainObjectToNewEntityTransformer (line 51-53), getEntityValue(object) is checked against
  !== undefined before setting values
• Previously, when object had a null property, getEntityValue would extract that null value (via
  entity[this.propertyName] at line 879), and the check would pass, setting null
• The actual impact is when the entire entity parameter is null/undefined - now it returns that
  value instead of undefined, which passes the !== undefined check

correctness

Code

src/metadata/ColumnMetadata.ts[774]

+        if (entity === undefined || entity === null) return entity
Evidence
The test case demonstrates the intended behavior change. The merge operation at line 28 passes `{
title: null }` and expects the title to be set to null (line 31). The
PlainObjectToNewEntityTransformer at lines 51-53 checks if (objectColumnValue !== undefined)
before setting values. With the change, when extracting a null property value from an object, the
method returns null (not undefined), which passes the check and sets the null value on the target
entity. This is the desired behavior per the test.

test/other-issues/merge-entity-with-null/merge-entity-with-null.test.ts[28-31]
src/query-builder/transformer/PlainObjectToNewEntityTransformer.ts[51-53]
src/metadata/ColumnMetadata.ts[879]


Grey Divider

Qodo Logo

@alumni
alumni merged commit 1676484 into typeorm:master Jan 15, 2026
38 checks passed
@github-project-automation github-project-automation Bot moved this from Proposed to Done in TypeORM Roadmap Jan 15, 2026
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
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.

5 participants