question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LeftJoinAndSelect is not working when using SubQueryFactory

See original GitHub issue

Issue type:

  • documentation issue
  • bug report

Database system/driver:

  • mysql / mariadb

TypeORM version:

  • latest 0.2.26

What is the proper way to create the following query?

SELECT user.*, photos.*
FROM user
LEFT JOIN 
    (SELECT * FROM user_photos ORDER BY updatedAt LIMIT 5) AS photos
    ON user.id = photos.userId

The best I have tried using SubQueryFactory

const result = await userRepo
      .createQueryBuilder('user')
      .leftJoinAndSelect(
        qb => qb
          .select()
          .from(UserPhotos, 'p')
          .orderBy({ 'p.updatedAt': 'ASC' })
          .limit(5),
        'photos'
      )
      .getMany()

This code generated this query (SIMPLIFIED)

SELECT user.*, photos.* 
FROM user
LEFT JOIN
    (SELECT * FROM `user_photos` `p` ORDER BY p.updatedAt ASC LIMIT 5) AS photos
    --- 🤨 where is ON clause?

Where is the ON clause 🤨?

You can see that everything is ok until the ON clause. When I don’t use SubQueryFactory everything is ok.

Not using SubQueryFactory (IS NOT A SOLUTION)

const result = await userRepo
      .createQueryBuilder('user')
      .leftJoinAndSelect('user.photos', 'photos')
      .getMany()

ON is right there

SELECT user.*, photos.* 
FROM user
LEFT JOIN `user_photos` `photos` 
    ON photos.userId = user.id --- see?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
vishal38-isharanicommented, Sep 26, 2020

@imnotjames this is working as intended, but when I get raw query result using getRawOne() it’s giving the joined data but when I use getOne() it’s returning blank data for that column. I even tried to use leftJoinAndMapOne but it’s still not working. https://github.com/typeorm/typeorm/issues/5637#issuecomment-699087519

2reactions
auragitcommented, May 19, 2022

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to use subquery in leftJoinAndSelect in TypeORM
I saw that .getMany() is not working when using SubQueryFactory . Insted you can use .getRawMany() .
Read more >
Is it possible to use subquery in leftJoinAndSelect in TypeORM
UPDATE. I saw that .getMany() is not working when using SubQueryFactory . Insted you can use .getRawMany() .
Read more >
https://unpkg.com/typeorm@0.2.7/query-builder/Sele...
leftJoinAndSelect (subQueryFactory: (qb: SelectQueryBuilder<any>) ... NOTE that it may not work as you expect if you are using joins.
Read more >
SelectQueryBuilder | typeorm
Executes sql generated by query builder and returns object with raw results and entities created from them. Parameters. queryRunner: QueryRunner. Returns ...
Read more >
TypeORM - Query Builder with Subquery - DEV Community ‍ ‍
Tagged with node, sql, typescript, database. ... However, this is not enough for creating graphs or displaying calculated results on the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found