Node.js, Prisma & PostgreSQL

Lesson 8 of 12

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.

📝 Include & Select Quiz

Passing score: 70%
  1. 1.What does the include option do?

  2. 2.Creating a user and a related post in a single call, via posts: { create: [...] }, is called a nested ____.

  3. 3.By default, Prisma Client automatically includes every related model on every query.