Next.js 15 Fullstack Web Apps · Lektion

Relationen und erweiterte Abfragen mit Prisma

Modellieren Sie Eins-zu-viele- und Viele-zu-viele-Relationen und fragen Sie diese anschließend mit verschachtelten Schreibvorgängen, include, select, Filtern und Paginierung ab.

Lektion 4 von 413 Schritte

Relationen und erweiterte Abfragen mit Prisma ist eine kostenlose Next.js 15 Fullstack Web Apps-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Next.js 15 Fullstack Web Apps-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Next.js 15 Fullstack Web Apps-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 with select
  • Nested writes create parents and children atomically
  • Filter on relations, paginate with take/skip or cursors, and avoid the N+1 problem
Kostenlos starten

Lerne TypeScript mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Relationen und erweiterte Abfragen mit Prisma“ kostenlos?

Ja — der vollständige Text von „Relationen und erweiterte Abfragen mit Prisma“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Next.js 15 Fullstack Web Apps-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Next.js 15 Fullstack Web Apps-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Relationen und erweiterte Abfragen mit Prisma“?

Modellieren Sie Eins-zu-viele- und Viele-zu-viele-Relationen und fragen Sie diese anschließend mit verschachtelten Schreibvorgängen, include, select, Filtern und Paginierung ab. Du übst Next.js 15 Fullstack Web Apps mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Next.js 15 Fullstack Web Apps zu starten?

Keine Vorkenntnisse erforderlich. Next.js 15 Fullstack Web Apps auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Relationen und erweiterte Abfragen mit Prisma“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Next.js 15 Fullstack Web Apps-Lektion Code schreiben und ausführen?

Ja. Jede Next.js 15 Fullstack Web Apps-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in Prisma ORM
  2. Prisma-Schemadesign und Migrationen
  3. CRUD-Operationen mit Prisma Client
  4. Relationen und erweiterte Abfragen mit Prisma
← Zurück zu Next.js 15 Fullstack Web Apps