Skip to content

TreeRepository's findRoots query incorrectly when using a custom primary key #6948

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

Related to #2361

When we use a custom primary column, the query breaks because it seems findRoots makes an assumption that the primary key = id.

An example if we use cat_id as the primary key, findRoots makes the following SQL query:

SELECT
    `treeEntity`.`cat_id` AS `treeEntity_cat_id`,
    `treeEntity`.`cat_name` AS `treeEntity_cat_name`,
    `treeEntity`.`mpath` AS `treeEntity_mpath`,
    `treeEntity`.`catParentCatId` AS `treeEntity_catParentCatId`
FROM
    `category` `treeEntity`
WHERE
    `treeEntity`.`catParentId` IS NULL '

Instead of the correct query:

SELECT
    `treeEntity`.`cat_id` AS `treeEntity_cat_id`,
    `treeEntity`.`cat_name` AS `treeEntity_cat_name`,
    `treeEntity`.`mpath` AS `treeEntity_mpath`,
    `treeEntity`.`catParentCatId` AS `treeEntity_catParentCatId`
FROM
    `category` `treeEntity`
WHERE
    `treeEntity`.`catParentCatId` IS NULL '

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