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.

Question: addSelect of leftJoins in SelectQueryBuilder

See original GitHub issue

Issue 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:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
anodynoscommented, Sep 2, 2019

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 with getMany(). In my specific case it’s a count(*) but FOO works well to demonstrate the issue.

The question is, can we include a custom field in the resulted object, using getMany (and not getRawMany 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:

  @Column({ nullable: true, select: false })
  foo: number;

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 with getMany, if it’s part of the SQL result.

1reaction
mschutttcommented, Jul 30, 2019

What’s the status on this one? I currently have the same problem… any workaround / fix?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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