Removing a nested-set ancestor and descendant together corrupts surviving tree bounds
Issue description
Removing a nested-set node and its descendant in the same remove([...]) call closes the same interval more than once. Surviving nodes receive invalid nsleft/nsright values, and later inserts can make a sibling appear as a descendant of the wrong node.
Expected Behavior
After removing branch and child, the remaining tree should be:
root [1, 6]
survivor [2, 5]
survivor child [3, 4]A subsequent node inserted under root should not be a descendant of survivor child.
Actual Behavior
The remaining bounds are:
root [1, 4]
survivor [2, 3]
survivor child [3, 4]After inserting new sibling under root, findDescendants(survivorChild) returns both survivor child and new sibling, even though the latter's parent is the root.
Steps to reproduce
Run this TypeScript example with TypeORM, sql.js, and reflect-metadata; enable experimentalDecorators and emitDecoratorMetadata.
import "reflect-metadata";
import {
DataSource, Entity, PrimaryGeneratedColumn, Column,
Tree, TreeParent, TreeChildren,
} from "typeorm";
@Entity()
@Tree("nested-set")
class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@TreeParent({ onDelete: "CASCADE" })
parent: Category | null;
@TreeChildren()
children: Category[];
}
async function main() {
const source = await new DataSource({
type: "sqljs", entities: [Category], synchronize: true,
}).initialize();
try {
const categories = source.getTreeRepository(Category);
const root = await categories.save({ name: "root" });
const branch = await categories.save({ name: "branch", parent: root });
const child = await categories.save({ name: "child", parent: branch });
const survivor = await categories.save({ name: "survivor", parent: root });
const survivorChild = await categories.save({ name: "survivor child", parent: survivor });
await categories.remove([branch, child]);
console.log(await source.query(
'SELECT name, nsleft, nsright FROM category ORDER BY nsleft',
));
await categories.save({ name: "new sibling", parent: root });
console.log("Survivor child's descendants:",
(await categories.findDescendants(survivorChild)).map(node => node.name),
);
} finally {
await source.destroy();
}
}
main().catch(console.error);Additional Context
The removal executor loads intervals ordered by their right boundary descending. It closes the ancestor's entire interval and then processes a contained descendant using its original interval, shifting some surviving boundaries a second time.
Skipping contained intervals fixes the bounds and the later incorrect descendant result. The scan is linear, keeps one previous interval, and avoids redundant UPDATE statements. Tests cover both input orders, simple/composite IDs, and an additional disjoint deletion.
I searched issues and open/closed PRs for nested-set removals and NestedSetSubjectExecutor. #7155/#7156 introduced update/delete support; #5483 concerns moving an entity through a loaded children collection; #11978 changes tree-level calculation. None covers this overlapping-removal trigger.
My Environment
| Dependency | Version |
|---|---|
| Operating system | Windows |
| Node.js | 22.17.1 |
| TypeScript | 5.9.3 |
| TypeORM | master at f279fd1367f24ad108a1b11cf833f1620274088d (package version 1.1.1) |
| SQL.js | 1.14.2 |
| better-sqlite3 | 12.11.1 |
Relevant Database Driver(s)
Reproduced with SQL.js and better-sqlite3. Other database engines were not run.
Are you willing to resolve this issue by submitting a Pull Request?
Yes; a fix and regression tests are ready.
Source: typeorm/typeorm