Prismaのリレーションと高度なクエリ
一対多および多対多のリレーションをモデル化し、ネストした書き込み、include、select、フィルタリング、ページネーションを使って関連データを横断的にクエリします。
「Prismaのリレーションと高度なクエリ」はCoddyKit上の無料Next.js 15 Fullstack Web Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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のリレーションと高度なクエリ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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のリレーションと高度なクエリ