Search code examples
typeorm

How to query against junction table directly


Let's use the TypeORM documentation's example of a many-to-many table:

@ManyToMany(type => Category)
@JoinTable({
    name: "question_categories", // table name for the junction table of this relation
    joinColumn: {
        name: "question",
        referencedColumnName: "id"
    },
    inverseJoinColumn: {
        name: "category",
        referencedColumnName: "id"
    }
})
categories: Category[];

Let's say I want to query directly against this table to run something like select * from question_categories where category = 3;. How do I express this with TypeORM's query builder? The table doesn't correspond to an entity so I don't know how to refer to it with the query builder API. Do I need to use entityManager.query for this?


Solution

  • I ended up creating a new entity for the junction table and querying directly against it that way.