LeftJoinAndSelect is not working when using SubQueryFactory
See original GitHub issueIssue 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:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
+1