Skip to content

fix: update child's mpath - #10844

Merged
gioboa merged 6 commits into
typeorm:masterfrom
JoseCToscano:fix/tree-repo-soft-deleted-parent-path
Jan 14, 2026
Merged

gioboa merged 6 commits into
typeorm:masterfrom
JoseCToscano:fix/tree-repo-soft-deleted-parent-path

Conversation

@JoseCToscano

@JoseCToscano JoseCToscano commented Apr 23, 2024 •

Copy link
Copy Markdown
Contributor

Include soft-deleted entities in queryBuilder to fetch required parent's path even if parent is soft deleted.
This path is required to perform the corresponding update of the childs' mpath

fix #10843

Description of change

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

Jose Toscano added 2 commits April 22, 2024 22:50
…pository

Include soft-deleted entities in queryBuilder to fetch required parent's path
even if parent is soft deleted. This path is required to perforn the corresponding
update of the childs' mpath

fix typeorm#10843
}
}),
)
.withDeleted()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the main change of the PR

@JoseCToscano

JoseCToscano commented Apr 24, 2024 •

Copy link
Copy Markdown
Contributor Author

Could we retry running the "failed" test? It seems it has timedout
Screenshot 2024-04-24 at 11 12 56 a m

@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 @JoseCToscano for your help
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 11 minutes and 35 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 a6e4a5b.

📒 Files selected for processing (3)
  • src/persistence/tree/MaterializedPathSubjectExecutor.ts (1 hunks)
  • test/github-issues/10843/entity/node.ts (1 hunks)
  • test/github-issues/10843/issue-10843.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@10844
    

commit: 42c14c9

@coveralls

coveralls commented Sep 21, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 80.906% (+0.03%) from 80.878%
when pulling 42c14c9 on JoseCToscano:fix/tree-repo-soft-deleted-parent-path
into 75f4463 on typeorm:master.

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

Copy link
Copy Markdown

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Impact
General
Reorder query builder method calls

Move the .withDeleted() method call to before the .where() call in the query
builder chain.

src/persistence/tree/MaterializedPathSubjectExecutor.ts [159-177]

 return this.queryRunner.manager
     .createQueryBuilder()
     .select(
         subject.metadata.targetName +
             "." +
             subject.metadata.materializedPathColumn!.propertyPath,
         "path",
     )
     .from(subject.metadata.target, subject.metadata.targetName)
+    .withDeleted()
     .where(
         new Brackets((qb) => {
             for (const data of normalized) {
                 qb.orWhere(new Brackets((qb) => qb.where(data)))
             }
         }),
     )
-    .withDeleted()
     .getRawOne()
     .then((result) => (result ? result["path"] : ""))

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 2

__

Why: The suggested reordering of withDeleted() and where() is a stylistic preference and does not affect the functionality, as TypeORM's query builder is not sensitive to the order of these specific methods.

Low
  • More

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

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@gioboa gioboa changed the title fix: Update child's mpath even if parentId was soft-deleted in TreeRepository fix: update child's mpath Jan 13, 2026
@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

Code Review by Qodo (Alpha)

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

Grey Divider


Action Required

1. NestedSet trees lack soft-delete handling 🐞 Bug
Description
• NestedSetSubjectExecutor has the same soft-delete bug as MaterializedPathSubjectExecutor but was
  not fixed in this PR
• When a parent node is soft-deleted and a child is reassigned to a new parent, queries for parent
  data will fail because they don't use .withDeleted()
• This affects both the insert method (lines 53-68) which queries parentNsRight, and the
  update method (lines 156-164) which calls getNestedSetIds to retrieve parent nested set values
• The bug will cause tree structure updates to fail or produce incorrect nested set values when
  parents are soft-deleted

correctness

Code

src/persistence/tree/MaterializedPathSubjectExecutor.ts[175]

+            .withDeleted()
Evidence
The PR adds .withDeleted() to MaterializedPathSubjectExecutor's getEntityPath method to handle
soft-deleted parents. NestedSetSubjectExecutor has parallel logic that queries parent data but lacks
this fix. In NestedSetSubjectExecutor.ts lines 53-68, the code queries parentNsRight from the
parent entity without .withDeleted(). Similarly, lines 156-164 call getNestedSetIds which also
queries without .withDeleted() (lines 285-308). Both will fail to retrieve data when parents are
soft-deleted, causing the same issue this PR fixes for materialized-path trees.

src/persistence/tree/MaterializedPathSubjectExecutor.ts[175-175]
src/persistence/tree/NestedSetSubjectExecutor.ts[53-68]
src/persistence/tree/NestedSetSubjectExecutor.ts[285-308]
test/github-issues/10843/issue-10843.test.ts[11-11]

Agent Prompt
## Issue Description
NestedSetSubjectExecutor needs the same soft-delete handling fix that was applied to MaterializedPathSubjectExecutor. When parent nodes are soft-deleted, queries for parent nested set values fail because they don't include soft-deleted records.

## Issue Context
The PR fixes MaterializedPathSubjectExecutor by adding `.withDeleted()` to the `getEntityPath` query at line 175. NestedSetSubjectExecutor has similar parent queries that need the same fix:
1. The `insert` method queries parent's right value to determine insertion point
2. The `update` method queries parent nested set IDs via `getNestedSetIds`
3. Both queries will return no results when parents are soft-deleted, breaking tree operations

## Fix Focus Areas
- src/persistence/tree/NestedSetSubjectExecutor.ts[53-68] - Add `.withDeleted()` before `.getRawOne()` in the parent query
- src/persistence/tree/NestedSetSubjectExecutor.ts[304-308] - Add `.withDeleted()` before `.getRawMany()` in the getNestedSetIds method

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


Grey Divider

Qodo Logo

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

Copy link
Copy Markdown

Persistent review updated to latest commit 42c14c9

@alumni

alumni commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator

I'm not sure if this won't break anything else, would be nice it someone that uses tree entities would check it.

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

It looks good to me, the change is minimal and we can always revert this change if needed.
Thanks @JoseCToscano for your patience and help.

@gioboa
gioboa merged commit 6f3788b into typeorm:master Jan 14, 2026
38 checks passed
mgohin pushed a commit to mgohin/typeorm that referenced this pull request 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.

TreeRepository does not update mpath if parentId was soft-deleted

5 participants