Question: addSelect of leftJoins in SelectQueryBuilder
See original GitHub issueIssue type:
[x] question [ ] bug report [ ] feature request [x] documentation issue
Database system/driver:
[x] postgres
TypeORM version:
[ ] latest
[ ] @next
[x] 0.2.5
(or put your version here)
Steps to reproduce or a small repository showing the problem:
I am trying to build the following query:
SELECT
"skill"."id",
"skill"."categoryId",
"categoryTranslation"."name" AS "category",
FROM skill "skill"
LEFT JOIN skill_category_translation "categoryTranslation"
ON "categoryTranslation"."categoryId" = "skill"."categoryId"
AND "categoryTranslation"."langCode" = 'de_DE'
using SelectQueryBuilder:
async findAll(langCode: string): Promise<Skill[]> {
return await this.repository.createQueryBuilder('skill')
.select('skill.id')
.addSelect('skill.categoryId')
.addSelect('categoryTranslation.name', 'category')
.leftJoin(
SkillCategoryTranslation,
'categoryTranslation',
'categoryTranslation.categoryId = skill.categoryId AND categoryTranslation.langCode = :langCode',
{ langCode },
)
.getMany();
}
but I get this query:
SELECT
"skill"."id" AS "skill_id",
"skill"."categoryId" AS "skill_categoryId",
"categoryTranslation"."name" AS "category",
"categoryTranslation"."categoryId" AS "categoryTranslation_categoryId",
"categoryTranslation"."langCode" AS "categoryTranslation_langCode"
FROM "skill" "skill"
LEFT JOIN "skill_category_translation" "categoryTranslation"
ON "categoryTranslation"."categoryId" = "skill"."categoryId"
AND "categoryTranslation"."langCode" = $1
-- PARAMETERS: ["de_DE"]
which doesn’t return any column from categoryTranslation
and only the two from skill
.
Any suggestions? I was looking into subQueries but it doesn’t seem to deliver the simplicity I need. Thanks a lot!
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Multiple JOIN with TYPEORM - Stack Overflow
Let's break them one by one, shall we? Assuming you know how then it would be simple: await getManager() .createQueryBuilder(table1, 't1') ...
Read more >typeorm/typeorm - Gitter
Hello, I have one fast question, how to get count of items in my relation orders ? my code: ... @Piotrek98 should that...
Read more >Joins and addSelect Reduce Queries - SymfonyCasts
In this case, each row in FortuneCookie will join to *one* Category. So, I would use an innerJoin. 2) fetch="EAGER" is ALMOST the...
Read more >SelectQueryBuilder | typeorm
Parameters. sort: string. Default value order: "ASC" | "DESC" = "ASC". Optional nulls: "NULLS FIRST" | "NULLS LAST". Returns this. addSelect.
Read more >typeorm select join column querybuilder - Code Grepper
leftJoin ('profile. ... Technical Problem Cluster First Answered On January 21, ... let query: SelectQueryBuilder<Projects> = this.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
On a more general note, I have a similar problem:
I add a virtual (i.e non existent field) such as
.addSelect("'FOO'", "Table_alias_foo")
and although I can see it in the SQL (and is returned properly when raw SQL is executed), it’s not returned in the resulted object withgetMany()
. In my specific case it’s acount(*)
butFOO
works well to demonstrate the issue.The question is, can we include a custom field in the resulted object, using
getMany
(and notgetRawMany
which is not very useful)?EDIT:
I found a workaround, by using a “virtual column” - it’s not very elegant and scaleable, but it works: Basically all you need is to have “foo” as a column in the model, ideally with the same type you intend to have, and omit it from being selected ie:
Essentially this creates a placeholder for
getMany
to know what to include in the object.It would be great if there was an annotation for this usecase, eg
@VirtualColumn
that doesn’t actually generate the column in the db schema, but knows that it should be included in the returned object withgetMany
, if it’s part of the SQL result.What’s the status on this one? I currently have the same problem… any workaround / fix?