Querying Relations with include and select
By default, Prisma Client only returns a model's own columns, related data has to be requested explicitly.
// Fetch a user together with their posts
const userWithPosts = await prisma.user.findUnique({
where: { id: 1 },
include: { posts: true },
});
// Shape exactly which fields come back
const summaries = await prisma.post.findMany({
select: {
title: true,
author: { select: { name: true } },
},
});
// Create a user and their first post in one call
const newUser = await prisma.user.create({
data: {
email: 'grace@example.com',
name: 'Grace',
posts: { create: [{ title: 'Hello, Prisma!' }] },
},
});
include adds related records on top of all normal fields, select replaces the default fields entirely, letting you return only what you need. A nested write like the posts: { create: [...] } above creates both rows in a single call.