Relations (OneToMany, ManyToOne, ManyToMany) in NestJS
📖 Scenario: You are building a simple blog application backend using NestJS and TypeORM. You need to set up the database models to represent authors, posts, and categories with proper relationships.
🎯 Goal: Create three entities: Author, Post, and Category with these relations:- Each Author can have many Posts (OneToMany)- Each Post belongs to one Author (ManyToOne)- Each Post can have many Categorys and each Category can have many Posts (ManyToMany)
📋 What You'll Learn
Create an Author entity with id and name fields
Create a Post entity with id, title, and content fields
Create a Category entity with id and name fields
Set up OneToMany and ManyToOne relations between Author and Post
Set up ManyToMany relation between Post and Category with a join table
💡 Why This Matters
🌍 Real World
Blog platforms, content management systems, and many apps need to model relationships between users, posts, and tags or categories.
💼 Career
Understanding entity relations is essential for backend developers working with databases and ORMs like TypeORM in NestJS.
Progress0 / 4 steps
1
Create the Author entity
Create a NestJS entity class called Author with two properties: id as a primary generated column and name as a string column.
NestJS
Hint
Use @Entity() decorator for the class, @PrimaryGeneratedColumn() for id, and @Column() for name.
2
Create the Post entity with author relation
Create a NestJS entity class called Post with id as a primary generated column, title and content as string columns. Add a ManyToOne relation to Author named author with the inverse side as posts.
NestJS
Hint
Use @ManyToOne in Post and @OneToMany in Author to link them.
3
Create the Category entity
Create a NestJS entity class called Category with id as a primary generated column and name as a string column.
NestJS
Hint
Use @Entity(), @PrimaryGeneratedColumn(), and @Column() decorators.
4
Add ManyToMany relation between Post and Category
In the Post entity, add a ManyToMany relation to Category named categories with a join table. In the Category entity, add the inverse ManyToMany relation to Post named posts.
NestJS
Hint
Use @ManyToMany on both sides and @JoinTable() on the owning side (Post).
Practice
(1/5)
1. In NestJS using TypeORM, which decorator correctly defines a OneToMany relationship from an Author entity to a Book entity?
easy
A. @OneToMany(() => Book, book => book.author)
B. @ManyToOne(() => Book, book => book.author)
C. @ManyToMany(() => Book, book => book.author)
D. @OneToOne(() => Book, book => book.author)
Solution
Step 1: Understand the relationship direction
A OneToMany relationship means one Author has many Books.
Step 2: Identify the correct decorator syntax
The decorator @OneToMany(() => Book, book => book.author) correctly sets this relation from Author to Book.
Final Answer:
@OneToMany(() => Book, book => book.author) -> Option A
Quick Check:
OneToMany from Author to Book = @OneToMany [OK]
Hint: OneToMany decorator links one entity to many others [OK]
Common Mistakes:
Confusing OneToMany with ManyToOne
Using ManyToMany instead of OneToMany
Incorrect arrow function syntax in decorator
2. Which of the following is the correct syntax to define a ManyToOne relation in a NestJS entity?
easy
A. @OneToOne(type => User, user => user.posts)
B. @OneToMany(type => User, user => user.posts)
C. @ManyToOne(type => User, user => user.posts)
D. @ManyToMany(type => User, user => user.posts)
Solution
Step 1: Recognize the relation type
ManyToOne means many posts belong to one user.
Step 2: Confirm correct decorator syntax
@ManyToOne(type => User, user => user.posts) is the correct way to define this relation.
Final Answer:
@ManyToOne(type => User, user => user.posts) -> Option C
Quick Check:
ManyToOne relation uses @ManyToOne decorator [OK]
Hint: ManyToOne decorator links many items to one parent [OK]
Common Mistakes:
Using OneToMany instead of ManyToOne
Wrong arrow function parameter name
Missing inverse side property
3. Given these entity relations in NestJS with TypeORM:
Hint: Always initialize OneToMany arrays to avoid runtime errors [OK]
Common Mistakes:
Assuming TypeORM auto-initializes arrays
Ignoring runtime errors from undefined arrays
Confusing property initialization with database schema
5. You want to model a social media app where users can follow many other users and be followed by many users. Which NestJS TypeORM relation setup correctly models this ManyToMany self-referencing relation?
hard
A.
@Entity()
class User {
@ManyToMany(() => User, user => user.followers)
@JoinTable()
following: User[];
@ManyToMany(() => User, user => user.following)
followers: User[];
}
B.
@Entity()
class User {
@OneToMany(() => User, user => user.followers)
following: User[];
@ManyToOne(() => User, user => user.following)
followers: User[];
}
C.
@Entity()
class User {
@ManyToOne(() => User, user => user.followers)
following: User[];
@OneToMany(() => User, user => user.following)
followers: User[];
}
D.
@Entity()
class User {
@ManyToMany(() => User)
@JoinTable()
following: User[];
@ManyToMany(() => User)
followers: User[];
}
Solution
Step 1: Understand self-referencing ManyToMany
Users follow many users and are followed by many users, so a ManyToMany self-relation is needed.
Step 2: Correctly set inverse sides and JoinTable
@Entity()
class User {
@ManyToMany(() => User, user => user.followers)
@JoinTable()
following: User[];
@ManyToMany(() => User, user => user.following)
followers: User[];
}
uses @ManyToMany with inverse sides and @JoinTable on one side, correctly modeling the relation.
Final Answer:
Code snippet with @ManyToMany, inverse sides, and @JoinTable -> Option A
Quick Check:
Self-referencing ManyToMany needs inverse sides and one @JoinTable [OK]
Hint: Self ManyToMany needs inverse sides and one @JoinTable [OK]
Common Mistakes:
Using OneToMany or ManyToOne for many-to-many
Missing inverse side property
Missing @JoinTable on one side
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.