Many-to-many relations

What are many-to-many relations?

Many-to-many is a relation where A contains multiple instances of B, and B contains multiple instances of A. Let's take for example Question and Category entities. A question can have multiple categories, and each category can have multiple questions.

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"

@Entity()
export class Category {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    name: string
}
import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    ManyToMany,
    JoinTable,
} from "typeorm"
import { Category } from "./Category"

@Entity()
export class Question {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    text: string

    @ManyToMany(() => Category)
    @JoinTable()
    categories: Category[]
}

@JoinTable() is required for @ManyToMany relations. You must put @JoinTable on one (owning) side of relation.

This example will produce following tables:

Saving many-to-many relations

With cascades enabled, you can save this relation with only one save call.

Deleting many-to-many relations

With cascades enabled, you can delete this relation with only one save call.

To delete a many-to-many relationship between two records, remove it from the corresponding field and save the record.

This will only remove the record in the join table. The question and categoryToRemove records will still exist.

Soft Deleting a relationship with cascade

This example shows how the cascading soft delete behaves:

In this example we did not call save or softRemove for category1 and category2, but they will be automatically saved and soft-deleted when the cascade of relation options is set to true like this:

Loading many-to-many relations

To load questions with categories inside you must specify the relation in FindOptions:

Or using QueryBuilder you can join them:

With eager loading enabled on a relation, you don't have to specify relations in the find command as it will ALWAYS be loaded automatically. If you use QueryBuilder eager relations are disabled, you have to use leftJoinAndSelect to load the relation.

Bi-directional relations

Relations can be uni-directional and bi-directional. Uni-directional relations are relations with a relation decorator only on one side. Bi-directional relations are relations with decorators on both sides of a relation.

We just created a uni-directional relation. Let's make it bi-directional:

We just made our relation bi-directional. Note that the inverse relation does not have a @JoinTable. @JoinTable must be only on one side of the relation.

Bi-directional relations allow you to join relations from both sides using QueryBuilder:

Many-to-many relations with custom properties

In case you need to have additional properties in your many-to-many relationship, you have to create a new entity yourself. For example, if you would like entities Question and Category to have a many-to-many relationship with an additional order column, then you need to create an entity QuestionToCategory with two ManyToOne relations pointing in both directions and with custom columns in it:

Additionally you will have to add a relationship like the following to Question and Category:

Last updated