使用 Prisma 处理关系与高级查询
建模一对多和多对多关系,然后使用嵌套写入、include、select、筛选和分页跨关系查询数据。
使用 Prisma 处理关系与高级查询 是 CoddyKit 上的免费 Next.js 15 Fullstack Web Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack Web Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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])
}Many-to-Many
Prisma supports implicit many-to-many with just array fields on both sides; it manages the join table for you.
model Post { id Int @id @default(autoincrement()) tags Tag[] }
model Tag { id Int @id @default(autoincrement()) posts Post[] }Including Related Data
By default queries return only the model fields. Use include to fetch related records too.
const user = await prisma.user.findUnique({
where: { id: 1 },
include: { posts: true },
});Selecting Specific Fields
Use select to fetch only the columns you need — leaner payloads and faster queries. You can nest select for relations.
const posts = await prisma.post.findMany({
select: { id: true, title: true, author: { select: { name: true } } },
});Nested Writes
Create a parent and its children in one call with a nested create. Prisma runs it in a transaction.
await prisma.user.create({
data: {
name: 'Ada',
posts: { create: [{ title: 'Hello' }, { title: 'World' }] },
},
});Filtering with where
Build rich filters with operators like contains, gt, in, and combine them with AND/OR.
const recent = await prisma.post.findMany({
where: {
published: true,
title: { contains: 'Prisma' },
},
});Filtering on Relations
You can filter a parent by a condition on its children using some, every, or none.
const authors = await prisma.user.findMany({
where: { posts: { some: { published: true } } },
});Sorting and Pagination
Order results with orderBy and page with take/skip (offset) for classic pagination.
const page = await prisma.post.findMany({
orderBy: { createdAt: 'desc' },
take: 10,
skip: 20,
});Cursor Pagination
For large or live datasets, cursor-based pagination is more stable than offset. Pass the last seen id as a cursor.
const next = await prisma.post.findMany({
take: 10,
cursor: { id: lastId },
skip: 1,
});Avoiding N+1 Queries
Fetching a list and then querying each item separately causes the N+1 problem. Prisma avoids it: use a single include to load relations in one optimized query rather than looping.
Quick Check
Test your Prisma relations knowledge.
Recap
You learned relational querying with Prisma:
- Model one-to-many and many-to-many relations in the schema
- Fetch relations with
include; trim fields withselect - Nested writes create parents and children atomically
- Filter on relations, paginate with take/skip or cursors, and avoid the N+1 problem
常见问题解答
「使用 Prisma 处理关系与高级查询」课时是免费的吗?
是的 — 「使用 Prisma 处理关系与高级查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack Web Apps 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。
「使用 Prisma 处理关系与高级查询」这节课中我会学到什么?
建模一对多和多对多关系,然后使用嵌套写入、include、select、筛选和分页跨关系查询数据。 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack Web Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Next.js 15 Fullstack Web Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack Web Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 Prisma 处理关系与高级查询」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Next.js 15 Fullstack Web Apps 课中编写并运行代码吗?
能。每节 Next.js 15 Fullstack Web Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Prisma ORM 简介
- Prisma 模式设计与迁移
- 使用 Prisma Client 执行 CRUD 操作
- 使用 Prisma 处理关系与高级查询