Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ilyasfoo opened this issue Oct 21, 2020 · 0 comments · Fixed by #6982
Closed

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

ilyasfoo opened this issue Oct 21, 2020 · 0 comments · Fixed by #6982

Comments

@ilyasfoo
Copy link
Contributor

ilyasfoo commented Oct 21, 2020

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 '
@ilyasfoo ilyasfoo changed the title TreeRepository's findRoots query from property name instead of database column name TreeRepository's findRoots query incorrectly when using a custom primary key Oct 28, 2020
ilyasfoo added a commit to ilyasfoo/typeorm that referenced this issue Oct 28, 2020
pleerock pushed a commit that referenced this issue Oct 30, 2020
* test: added failing test for github issue #6948

* fix: findRoots should get the actual primary key column, fixes #6948
dolsup pushed a commit to dolsup/typeorm that referenced this issue Nov 3, 2020
* test: added failing test for github issue typeorm#6948

* fix: findRoots should get the actual primary key column, fixes typeorm#6948
zaro pushed a commit to zaro/typeorm that referenced this issue Jan 12, 2021
* test: added failing test for github issue typeorm#6948

* fix: findRoots should get the actual primary key column, fixes typeorm#6948
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant