Shamoon
Shamoon

Reputation: 43491

How can I join multiple tables with TypeORM leftJoinAndSelect?

I have:

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

I want to also get all invitations that match that company. My invitation model has:

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

Upvotes: 2

Views: 789

Answers (1)

DemiPixel
DemiPixel

Reputation: 1881

Did you try this?

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

Your Company entity should have:

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

Upvotes: 1

Related Questions