Closure-table reparenting leaves stale ancestors when a primary column is renamed
Issue description
Moving a closure-table branch with a renamed primary database column can retain its old ancestors. Moving within the same tree can also fail with a unique-constraint error.
Expected Behavior
When branch -> leaf moves from old root to new root, the leaf's ancestors should be exactly branch, leaf, and new root.
Actual Behavior
findAncestors(leaf) also returns old root. The direct parent has changed, but the old closure-table relationships remain.
This requires the primary property's name to differ from its database column name, such as categoryId mapped to category_id.
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("closure-table")
class Category {
@PrimaryGeneratedColumn({ name: "category_id" })
categoryId: number;
@Column()
name: string;
@TreeParent()
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 oldRoot = await categories.save({ name: "old root" });
const newRoot = await categories.save({ name: "new root" });
const branch = await categories.save(
categories.create({ name: "branch", parent: oldRoot }),
);
const leaf = await categories.save({ name: "leaf", parent: branch });
branch.parent = newRoot;
await categories.save(branch);
console.log("Leaf ancestors:",
(await categories.findAncestors(leaf)).map(node => node.name).sort(),
);
} finally {
await source.destroy();
}
}
main().catch(console.error);Current output:
Leaf ancestors: [ 'branch', 'leaf', 'new root', 'old root' ]Expected:
Leaf ancestors: [ 'branch', 'leaf', 'new root' ]Additional Context
The DELETE phase of ClosureSubjectExecutor.update() reads the entity with entity[column.databaseName]. Hydrated entity objects use property names, so that lookup produces an undefined value. The subsequent INSERT phase already uses column.getEntityValue(entity).
Using the metadata getter for the DELETE parameters fixes the mismatch without changing the SQL shape or adding queries. Regression tests cover cross-root moves, same-tree moves, and detaching a branch as a root, including a descendant.
I checked existing issues and PRs for closure-table moves and renamed primary columns. #5404 concerns descendant-tree hydration, #7069 concerns naming closure-table columns, and #11978 changes tree-level calculation. These do not repair this primary-value lookup in the deletion phase.
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