【问题标题】:How can I join multiple tables with TypeORM leftJoinAndSelect?如何使用 TypeORM leftJoinAndSelect 连接多个表?
【发布时间】:2021-11-15 22:49:52
【问题描述】:

我有:

this.createQueryBuilder('company')
      .leftJoinAndSelect('company.companyAdmins', 'companyAdmins')
      .leftJoinAndSelect('companyAdmins.user', 'user')
      .where('company.id = :id', { id })
      .getOne();

我还想获得与company 匹配的所有invitations。我的invitation 模型有:

@Index()
  @ManyToOne(() => Company)
  public company: Company;

【问题讨论】:

  • Company 模型中的邀请关系是否有 @OneToMany

标签: javascript postgresql typeorm


【解决方案1】:

你试过了吗?

this.createQueryBuilder('company')
  .leftJoinAndSelect('company.companyAdmins', 'companyAdmins')
  .leftJoinAndSelect('companyAdmins.user', 'user')
  .leftJoinAndSelect('company.invites', 'invites') // Adding this
  .where('company.id = :id', { id })
  .getOne();

您的公司实体应具有:

@OneToMany(
  type => Invite,
  invite => invite.company,
)
invites?: Invite[];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多