typeorm: leftJoinAndMapMany is not mapping the result to the property when I pass a Subquery to the second argument

Issue type: [X] bug

Database system/driver: [X] postgres

TypeORM version: [X] latest

Steps to reproduce:

  1. Clone this repo
  2. npm install && tsc
  3. npm start

These are the entities:

@Entity()
export class Category {
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    name: string;

    posts: Post[];
}

@Entity()
export class Post {
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    title: string;
    @Column('int')
    categoryId: number;
}

And this is how I build the queries:

This one returns the list of Posts:

connection.createQueryBuilder().select('category').from(Category, 'category')
              .leftJoinAndMapMany('category.posts', Post, 'post', 'post.categoryId = category.id');

This one does not:

connection.createQueryBuilder().select('category').from(Category, 'category')
                    .leftJoinAndMapMany('category.posts', qb => qb.select().from(Post, 'post'), 'post', 'post."categoryId" = category.id');

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (3 by maintainers)

Most upvoted comments

I’m going to deprecate joinAndMap methods in 0.4.0 and don’t recommend to use them. Alternative is to execute a separate query, if you need to have it in the same query consider using raw query.

Is there any solution?

Same issue here.

I found the difference from loggings of getRawOne() when subquery is passed or not to leftJoinAndMapMany.

I think it maybe related mapping issue.

getRawOne(or getRawMany) result has not prefix of join target table when using subquery. While without subquery, We can find prefix.

ex) getRawOne result without subquery is

category_id : 1  // catggory id
category_name : 'test category' // category name
id : 1 // post id
title : 'test post' // post title

and with subquery we can find the prefix for pos

category_id : 1  // catggory id
category_name : 'test category' // category name
post_id : 1 // post id
post_title : 'test post' // post title

and each other queries which can be found from getQuery() result are same.

Is deprecating the joinAndMap() methods still on the table? If so, could you please update the roadmap accordingly?

I didn’t find a test with leftJoinAndMapMany using a Subquery instead of an Entity, but in this comment @pleerock says it should work, if I understood it correctly. It is also written that it should work in the docs here

What will replace it in the future