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

loadRelationCountAndMap fails cause made a wrong IN calculation, when primary key is string #3946

Closed
minzojian opened this issue Apr 6, 2019 · 2 comments · Fixed by #4169

Comments

@minzojian
Copy link

minzojian commented Apr 6, 2019

Issue type:

[ ] question
[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:

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

Steps to reproduce or a small repository showing the problem:

I have 3 tables, unit,word, and unit_words which is records the unit and word's many to many relationship.
i am going to selelect one unit and the count of the words in it. i found the loadRelationCountAndMap method,looks it can help me.so i write code like this:

  const loadedUnit = await connection.manager
            .createQueryBuilder(Unit, "unit")
            .leftJoinAndSelect("unit.words", "words")
            .loadRelationCountAndMap("unit.wordsCount", "unit.words")
            .where("unit.unit_id=:unitId",{unitId:"0fa1c80c-7a80-4809-84c6-daa9d59dccd5"})
            .getManyAndCount();

why i run the code ,i got an sql error, and i found the sql typeorm generate is like this

SELECT `words_rc`.`unit_id` AS `parentId`, COUNT(`word`.`word_id`) AS `cnt` FROM `word` `word` 
INNER JOIN `unit_words` `words_rc` ON `words_rc`.`unit_id` 
IN (0fa1c80c-7a80-4809-84c6-daa9d59dccd5,1943c3eb-3a6e-450d-8445-704a3dd795fd,46e4a934-fa40-4c82-ab2a-272138c2835a)
 AND `words_rc`.`word_id` = word.word_id GROUP BY `words_rc`.`unit_id`

problem is on IN (0fa1c80c-7a80-4809-84c6-daa9d59dccd5,1943c3eb-3a6e-450d-8445-704a3dd795fd,46e4a934-fa40-4c82-ab2a-272138c2835a)

it seems should to be IN ("0fa1c80c-7a80-4809-84c6-daa9d59dccd5","1943c3eb-3a6e-450d-8445-704a3dd795fd","46e4a934-fa40-4c82-ab2a-272138c2835a")

why that? is that a bug or some I did wrong?
looks this post is a similar issue
#2338

@Kononnable
Copy link
Contributor

Well, we didn't publish version 6.0.0 yet. We would also need entity definition, to see if something isn't mixed up there.

@AntonioAngelino
Copy link
Contributor

I can confirm the bug. We had the same issue because we use generated uuids as FK.

pleerock added a commit that referenced this issue May 30, 2019
…tAndMap

Fixes #3946 - loadRelationCountAndMap fails when entities' primary keys are strings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants