Next.js 15 Fullstack (App Router + Server Actions) · Lektion

Migrationen von Datenbankschemas

Verwalten Sie Änderungen an Ihrem Datenbankschema mit Prisma Migrate für eine reibungslose Entwicklung und Bereitstellung.

Lektion 3 von 411 Schritte

Migrationen von Datenbankschemas ist eine kostenlose Next.js 15 Fullstack (App Router + Server Actions)-Lektion auf CoddyKit. Dies ist Lektion 3 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 (App Router + Server Actions)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.

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

What are Database Migrations?

As your application evolves, so does its database structure, also known as its schema.

Database migrations are like version control for your database schema. They are a way to manage changes to your database structure in a controlled and trackable manner.

  • They ensure your database matches your application's needs.
  • They prevent data loss during schema changes.
  • They provide a clear history of all schema modifications.

Why Use Prisma Migrate?

Managing database schema changes manually can be complex and error-prone. Prisma Migrate simplifies this process significantly.

  • Automatic Generation: It generates SQL migration files based on changes in your Prisma schema.
  • History Tracking: It keeps a history of applied migrations, making rollbacks and team collaboration easier.
  • Environment Consistency: Ensures your development, staging, and production databases all have the correct schema.

Your Prisma Schema File

Prisma Migrate uses your schema.prisma file as the source of truth for your database schema. Any changes you want to make to your database structure start here.

Let's recall a basic schema.prisma setup:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  name  String
  posts Post[]
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  author    User     @relation(fields: [authorId], references: [id])
  authorId  Int
}

Initializing Your First Migration

When you first set up your Prisma schema, you'll want to create an initial migration to apply this schema to your database. You use the prisma migrate dev command for this.

This command does two main things:

  1. Generates a new migration file (SQL) in the prisma/migrations folder.
  2. Applies the migration to your database.
npx prisma migrate dev --name initial_setup

Understanding Migration Files

After running npx prisma migrate dev, Prisma creates a new folder inside prisma/migrations with a timestamp and the name you provided (e.g., 20231026123456_initial_setup).

Inside this folder, you'll find:

  • migration.sql: This file contains the raw SQL commands generated by Prisma to create or alter your database tables.
  • migration.js or migration.ts: (If using TypeScript/JavaScript) Metadata about the migration.

It's important to commit these files to your version control system!

Making Schema Changes

Now, let's say you want to add an email field to your User model. You simply update your schema.prisma file.

Prisma will detect this change and help you generate a new migration.

model User {
  id    Int     @id @default(autoincrement())
  name  String
  email String  @unique // Added new field
  posts Post[]
}

Generating a New Migration

After modifying your schema.prisma, run npx prisma migrate dev again. Prisma will compare your updated schema with the current database state and generate a new migration file.

It will then apply this new migration, adding the email column to your User table.

npx prisma migrate dev --name add_user_email

Applying Migrations in Production

For production environments, you typically use npx prisma migrate deploy. This command applies all pending migrations that have not yet been run on the database.

Unlike migrate dev, deploy does not generate new migration files; it only applies existing ones. This makes it safe and predictable for production deployments.

npx prisma migrate deploy

Checking Migration Status

To see which migrations have been applied to your database and which are pending, you can use the npx prisma migrate status command.

This is useful for debugging or ensuring your database is up-to-date with your application's expected schema.

npx prisma migrate status

Prisma Migrate Commands Quiz

Which of the following statements correctly describe the purpose of Prisma Migrate commands?

Recap: Schema Migrations

Great job! You've learned how to manage your database schema effectively using Prisma Migrate.

  • Prisma Migrate helps version control your database schema.
  • You define schema changes in schema.prisma.
  • npx prisma migrate dev creates and applies migrations in development.
  • npx prisma migrate deploy applies existing migrations in production.
  • npx prisma migrate status checks the current migration state.

This powerful tool ensures your database structure stays consistent and aligned with your application's needs as it grows.

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
22
Lektionen
88

Häufig gestellte Fragen

Ist die Lektion „Migrationen von Datenbankschemas“ kostenlos?

Ja — der vollständige Text von „Migrationen von Datenbankschemas“ 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 (App Router + Server Actions)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Migrationen von Datenbankschemas“?

Verwalten Sie Änderungen an Ihrem Datenbankschema mit Prisma Migrate für eine reibungslose Entwicklung und Bereitstellung. Du übst Next.js 15 Fullstack (App Router + Server Actions) 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 (App Router + Server Actions) zu starten?

Keine Vorkenntnisse erforderlich. Next.js 15 Fullstack (App Router + Server Actions) 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 3 von 4.

Wie lange dauert die Lektion „Migrationen von Datenbankschemas“?

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 (App Router + Server Actions)-Lektion Code schreiben und ausführen?

Ja. Jede Next.js 15 Fullstack (App Router + Server Actions)-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. Prisma ORM einrichten
  2. CRUD mit Server Actions
  3. Migrationen von Datenbankschemas
  4. Datenbank-Seeding und Connection Pooling
← Zurück zu Next.js 15 Fullstack (App Router + Server Actions)