Home > Blockchain >  TypeOrm Postgres two level nesting
TypeOrm Postgres two level nesting

Time:07-11

Customer has 0 or more OilSeparator, and OilSeparator has 0 or more Inspection. How do I fetch a customer with it's OilSeparators that in turn has it's Inspections?

I want to do this:

this.customerRepository.findOne({
      where: { id },
      relations: ['oilSeparators, oilSeparators.inspections'],
    });

but it's not supported as far as I can see. Do you know how to do this with createQueryBuilder ?

CodePudding user response:

you need to use queryBuilder example :

this.customerRepository 
      .createQueryBuilder('customer')
      .innerJoinAndSelect('customer.oilSeparators', 'oilSeparators')
      .innerJoin("oilSeparators.inspections", "inspections")
      .getRawOne()

or use eager: true in inspections Entity

also you can use .relation(OilSeparator, "customer.OilSeparator.inspections")

if you need moro information about join read this

  • Related