0
0
NestJSframework~5 mins

Relations (OneToMany, ManyToOne, ManyToMany) in NestJS

Choose your learning style9 modes available
Introduction

Relations help connect data between tables so you can organize and access related information easily.

When you want to link users to their posts in a blog app.
When you need to connect orders to customers in a shopping system.
When you want to track which students are in which classes.
When you want to manage tags that can belong to many articles.
When you want to keep track of comments related to a specific post.
Syntax
NestJS
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, ManyToMany, JoinTable } from 'typeorm';

@Entity()
export class ParentEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToMany(() => ChildEntity, child => child.parent)
  children: ChildEntity[];
}

@Entity()
export class ChildEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => ParentEntity, parent => parent.children)
  parent: ParentEntity;
}

@Entity()
export class EntityA {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToMany(() => EntityB)
  @JoinTable()
  relatedEntities: EntityB[];
}

@Entity()
export class EntityB {
  @PrimaryGeneratedColumn()
  id: number;
}

@OneToMany and @ManyToOne always work together to link two entities.

@ManyToMany needs a @JoinTable on one side to create the connection table.

Examples
This example shows authors having many books, and each book belongs to one author.
NestJS
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from 'typeorm';

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToMany(() => Book, book => book.author)
  books: Book[];
}

@Entity()
export class Book {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => Author, author => author.books)
  author: Author;
}
This example shows students and courses connected many-to-many, meaning students can join many courses and courses can have many students.
NestJS
import { Entity, PrimaryGeneratedColumn, ManyToMany, JoinTable } from 'typeorm';

@Entity()
export class Student {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToMany(() => Course)
  @JoinTable()
  courses: Course[];
}

@Entity()
export class Course {
  @PrimaryGeneratedColumn()
  id: number;
}
Sample Program

This program defines three entities: User, Post, and Tag. Users can have many posts (OneToMany). Each post belongs to one user (ManyToOne). Posts can have many tags and tags can belong to many posts (ManyToMany).

NestJS
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, ManyToMany, JoinTable } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(() => Post, post => post.user)
  posts: Post[];
}

@Entity()
export class Post {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @ManyToOne(() => User, user => user.posts)
  user: User;

  @ManyToMany(() => Tag)
  @JoinTable()
  tags: Tag[];
}

@Entity()
export class Tag {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;
}
OutputSuccess
Important Notes

Always define both sides of OneToMany and ManyToOne relations for proper data access.

Use @JoinTable only on one side of ManyToMany to avoid errors.

Relations help keep your data organized and easy to query.

Summary

Relations connect different data tables to show how they relate.

OneToMany and ManyToOne link one item to many others and back.

ManyToMany connects many items on both sides with a join table.