Many-to-Many
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 contain multiple instances of A. Let's take for example Question and Category entities. 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 show what the cascading soft deletes behaves
As you can see 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 question with categories inside you must specify relation in FindOptions:
Or using QueryBuilder you can join them:
When using FindOptions you don't need to specify eager relations - they are always automatically loaded.
bi-directional relations
Relations can be uni-directional and bi-directional. Uni-directional are relations with a relation decorator only on one side. Bi-directional 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, 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 to your many-to-many relationship you have to create a new entity yourself. For example if you would like entities Post and Category to have a many-to-many relationship with additional order column, you need to create entity PostToCategory with two ManyToOne relations pointing in both directions and custom columns in it:
Additionally you will have to add a relationship like the following to Post and Category:
Last updated
Was this helpful?