Skip to content

Custom primary column for TreeRepository based entities unable to get tree descendants #6947

@ilyasfoo

Description

@ilyasfoo

Issue type:

[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

  1. Create an entity with a custom primary column name
import { Entity, PrimaryGeneratedColumn, Column, Tree, TreeChildren, TreeParent } from "typeorm";

@Entity()
@Tree("closure-table")
export class TestEntity {
  @PrimaryGeneratedColumn()
  org_id: number;

  @Column()
  org_name: string;

  @TreeChildren()
  children: TestEntity[];

  @TreeParent()
  parent: TestEntity;
}
  1. Test by persisting a parent and a child and then list descendants
async function test() {
  // Get tree repository
  const repository = getManager().getTreeRepository(TestEntity);

  // Create parent node
  const parent = repository.create({
    org_id: 1234,
    org_name: "Parent test",
  });
  parent.save();

  // Create child node
  const child = repository.create({
    org_name: "Child test"
  });
  child.parent = parent;
  child.save();

  // List all descendants
  return await repository.findDescendantsTree(
    await repository.findOne({ org_id: 1234 })
  );
}

Expected:

{
  "org_id": 1234,
  "org_name": "Parent test",
  "children": [
    {
        "org_id": 1235,
        "org_name": "Child test",
        "children": []
    }
  ]
}

Instead:

{
  "org_id": 1234,
  "org_name": "Parent test",
  "children": []
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions