V
V
Vladimir Golub2021-07-05 17:51:15
Node.js
Vladimir Golub, 2021-07-05 17:51:15

How to forward a top-level alias in typeorm to a subquery?

I want to forward the alias from above so as not to specify the entity to select in the subquery

let query = this.articles.createQueryBuilder('artc')
      .select(['artc.title'])
      .addSelect(qb => qb
          .from(ArticlesEntity, 'artc')
          .select('name')
          .leftJoin(AuthorsEntity, 'a', 'a.id = artc.author_id')
      )


That doesn't work either
let query = this.articles.createQueryBuilder('artc')
      .select(['artc.title'])
      .addSelect(qb => qb
        .from(ArticlesEntity, 'artc1')
        .select('name')
        .leftJoin(AuthorsEntity, 'a', 'a.id = artc1.author_id AND artc1.id = artc.id')
      )

    //query = ArticlesEntity.authors(query, 'artc');

    return await query.getRawMany();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Golub, 2021-07-06
@RazerVG

working version

const query = getConnection()
        .createQueryBuilder()
        .select('title')
        .addSelect(qb => qb
            .select('name')
            .from(AuthorsEntity, 'aut')
            .leftJoin(ArticlesEntity, 'art1', 'aut.id = art1.author_id')
            .where('art1.id = art.id'),
          'name'
        )
        .from(ArticlesEntity, 'art');

    return query.getRawMany();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question