Relations and Advanced Queries with Prisma
Model one-to-many and many-to-many relations, then query across them with nested writes, include, select, filtering, and pagination.
Why Relations?
Real data is connected: a user has many posts, a post has many tags. Prisma models these relations in the schema so you can traverse them in type-safe queries.
One-to-Many in the Schema
A one-to-many relation links a parent to many children via a foreign key. Define both sides in the schema.
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
authorId Int
author User @relation(fields: [authorId], references: [id])
}All lessons in this course
- Introduction to Prisma ORM
- Prisma Schema Design and Migrations
- CRUD Operations with Prisma Client
- Relations and Advanced Queries with Prisma