leftJoinAndMapOne does not add target property to entity

See original GitHub issue

Issue type:

[X] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [X] postgres [ ] sqlite [ ] sqljs [ ] websql

TypeORM version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have a Source model which can have many Job models

I want to fetch a given source, and together with the most recently created job.

I use this query:

this
            .createQueryBuilder('source')
            .leftJoinAndMapOne('lastJob', subQuery => {
                return subQuery
                    .select()
                    .from(Job, 'job')
                    .orderBy('"createdAt"', 'DESC')
            }, 'job', 'job."sourceId" = source.id')
            .where('source.id = :id', { id })
            .getOne()

The above will return the Source, but without the lastJob property

If I you getRawOne

It returns the source properties together with the job properties, but they’re not scoped under lastJob

Any ideas?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
wi-skicommented, Apr 27, 2021

Still an issue in 2021

12reactions
FFsimcommented, Mar 7, 2020

same problem with code:

      .leftJoinAndMapOne("user.lastPurchase", (subQuery) => {
        return subQuery
          .select()
          .from(Purchase, "purchase")
          .orderBy("created", "DESC");
      }, "purchase", "purchase.userId = user.id")
      .leftJoinAndSelect("user.merchant", "merchant")
      .where("user.role IN (:userRoles)", {userRoles: roles})
      .getManyAndCount();

any idea?

Read more comments on GitHub >

github_iconTop Results From Across the Web

leftJoinAndMapOne does not add target property to entity #5637
leftJoinAndMapOne does not add target property to entity #5637 ... The above will return the User, but without the lastPurchase property.
Read more >
SelectQueryBuilder | typeorm
Contains all properties of the QueryBuilder that needs to be build a final ... Specifies FROM which entity's table select/update/delete will be executed....
Read more >
Is it possible to use subquery in leftJoinAndSelect in TypeORM
Using the SubQueryFactory option does not automatically create the on clause as the condition as it cannot know what the underlying query's ...
Read more >
TypeORM - Query Builder with Subquery - DEV Community ‍ ‍
This is the main part to demonstrate how we should build complex queries with TypeORM. Target SQL converted to TypeScript with TypeORM. SELECT ......
Read more >
Select using Query Builder - typeorm - GitBook
There are two types of results you can get using select query builder: entities or raw results. Most of the time, you need...
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