[英]TypeOrm QueryBuilder Multiple relations to one table
Thanks for reading and trying to help in advance!感谢您阅读并尝试提前提供帮助!
I have a following problem.我有以下问题。 On my "Offer" entity there are multiple relations to "User", as follows:
在我的“报价”实体上,与“用户”有多种关系,如下所示:
@ManyToOne(() => User, (user) => user.offers, { nullable: false })
owner: User;
@ManyToMany(() => User, (user) => user.participates, {
nullable: true,
cascade: true,
})
@JoinTable()
participants: User[];
@ManyToMany(() => User, (user) => user.applied, {
nullable: true,
cascade: true,
})
@JoinTable()
applicants: User[];
Question is - how do I load all of these relations with query builder?问题是 - 如何使用查询构建器加载所有这些关系? When I use leftJoinAndSelect it throws an error saying: "QueryFailedError: table name "user" specified more than once"
当我使用 leftJoinAndSelect 时,它会抛出一个错误:“QueryFailedError: 表名“user”指定了不止一次”
return await this.getOffersBaseQuery()
.andWhere('o.id = :id', {
id,
})
.leftJoinAndSelect('o.skill', 'skill')
.leftJoinAndSelect('o.owner', 'user')
.leftJoinAndSelect('o.participants', 'user')
.select([
'o',
'skill.id',
'skill.name',
'user.username',
'user.email',
'user.id',
])
.getOne();
Just in case - base query:以防万一 - 基本查询:
private getOffersBaseQuery() {
return this.offerRepository.createQueryBuilder('o').orderBy('o.id', 'DESC');
}
Nevermind, the answer to this question was just in TypeORM docs.没关系,这个问题的答案就在 TypeORM 文档中。 All I had to do was to use different allias for
我所要做的就是使用不同的别名
.leftJoinAndSelect('o.participants', 'user')
which I did with:我是这样做的:
.leftJoinAndSelect('o.participants', 'participants')
Feel really stupid now.现在觉得自己很傻。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.