Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
TypeORM: how to use In() with @ManyToMany relationship?

I have two entities: Something and Other (prototypes are below).

How can I use In() method to find all Somethings that have related Others by array of Others ids?

I need something like this:

const smths: Array<Something> = await connection.getRepository(Something).find({
    others: In(arrayWithOthersIds),
});

Entities:

@Entity()
class Something {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @ManyToMany(() => Other, (other) => other.smths, { cascade: true })
    others: Other[];
}

@Entity()
class Other {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @ManyToMany(() => Something, (smth) => smth.others, { cascade: true })
    smths: Something[];
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You must use @JoinTable() from 1 side of your many-to-many relationship. Also as I know you can not use { cascade: true } on both sides, so remove one. And use this query:

const smths: Something[] = await connection
   .getRepository(Something)
   .createQueryBuilder('smth')
   .innerJoin("smth.others", "others")
   .where('others.id IN (:...ids)', { ids } ) // ids = [1, 2..]
   .getMany();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!