0
0
NestJSframework~5 mins

Relations in Prisma in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a relation in Prisma?
A relation in Prisma connects two models, like a link between tables in a database. It helps you organize and access related data easily.
Click to reveal answer
intermediate
How do you define a one-to-many relation in Prisma schema?
You add a field with type of the related model and use @relation attribute. For example, a User has many Posts: <br>model User {<br> id Int @id @default(autoincrement())<br> posts Post[]<br>}<br>model Post {<br> id Int @id @default(autoincrement())<br> userId Int<br> user User @relation(fields: [userId], references: [id])<br>}
Click to reveal answer
beginner
What does the @relation attribute do in Prisma?
The @relation attribute tells Prisma how two models are connected. It specifies which fields link the models and which fields are referenced, making the relation clear.
Click to reveal answer
intermediate
How do you represent a many-to-many relation in Prisma?
You create two models with array fields of each other. Prisma automatically creates a join table. Example:<br>model Student {<br> id Int @id @default(autoincrement())<br> courses Course[]<br>}<br>model Course {<br> id Int @id @default(autoincrement())<br> students Student[]<br>}
Click to reveal answer
beginner
Why are relations important in Prisma when using NestJS?
Relations help NestJS apps fetch and manage connected data easily. They let you get related records in one query, making your app faster and simpler to write.
Click to reveal answer
In Prisma, how do you link a Post to a User in a one-to-many relation?
AAdd a posts field in User only
BUse @manyToOne attribute in Post
CAdd a user field in User model
DAdd a userId field in Post and use @relation to User
What does Prisma create automatically for many-to-many relations?
AA join table to connect the two models
BA single combined model
CNo extra tables are created
DA foreign key in one model only
Which Prisma attribute specifies how two models are connected?
A@link
B@connect
C@relation
D@join
In a one-to-many relation, which side holds the foreign key field?
AThe many side
BThe one side
CBoth sides
DNeither side
Why use relations in Prisma with NestJS?
ATo disable data fetching
BTo fetch related data easily and write simpler queries
CTo write more complex SQL manually
DTo avoid using databases
Explain how to set up a one-to-many relation between User and Post models in Prisma.
Think about which model holds the foreign key and how @relation connects them.
You got /4 concepts.
    Describe the difference between one-to-many and many-to-many relations in Prisma.
    Consider how data is connected and stored in the database.
    You got /4 concepts.