1
votes

Can someone tell me why I can't use relation in my createQuerybuilder:

let user = await this.conn.getRepository(UserEntity)
                          .createQueryBuilder('user')
                          .relation('orders')
                          .orderBy('user.id', 'ASC') // 'Property 'orderBy' does not exist on type 'RelationQueryBuilder<UserEntuty>.

my error is throwing at .orderBy, but is not problem with orderBy because when i put in this place a diffrent function, this error is still exists

thanks for any help

1
As I can see in documentation relation takes two argument. Relation FunctionxMayank
@xMayank i see, but when i add my entity into this relation, i have still this same erroruser13835196
Let's use leftJoin() or innerJoin() instead of relation(). It will be smth like that await this.conn.getRepository(UserEntity).createQueryBuilder('user').innerJoin('user.orders', 'order').orderBy('user.id', 'ASC')Art Olshansky

1 Answers

2
votes

I think this is what are you looking for:

const users = await this.conn.getRepository(UserEntity)
                              .createQueryBuilder('user')
                              .leftJoinAndSelect('user.orders', 'orders')
                              .orderBy('user.id', 'ASC')
                              .getMany();

In the code above you will get a an array of users. Each user has an array of orders. This is good for you?

See the docs here about joins in TypeORM.

It's better using joins for a SELECT operation rather than the relation method. Relation docs here.

Hope it helps :)