General
Database

Schema

Learn how to update your database schema and migrate changes with Prisma.

The database schema is defined in prisma/schema.prisma. This schema file uses Prisma's schema definition language (PSL) to describe your database tables, relationships, and types.

Schema Structure

The schema.prisma file contains:

  • Data source: Database connection configuration
  • Generator: Prisma Client configuration
  • Models: Database table definitions with relationships
prisma/schema.prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

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

model User {
  id    String @id @default(uuid())
  email String @unique
  name  String?
  // ... other fields
}

Updating the Schema

To update your database schema, edit the schema.prisma file. More information about the Prisma schema can be found in the Prisma documentation.

Example: Adding a Field

For example, to add a new phone field to the User model:

prisma/schema.prisma
model User {
  id    String  @id @default(uuid())
  email String  @unique
  name  String?
  phone String? // New field
  // ... other fields
}

The field is defined as an optional string. Now you need to create a migration to apply this change to the database.

Migration Workflow

Prisma uses migrations to track and apply database schema changes. The migration workflow consists of three steps:

1. Create Migration

Create a new migration by running:

Terminal
npx prisma migrate dev --name add_phone_field

This command:

  • Analyzes your schema.prisma file
  • Compares it with the current database state
  • Generates SQL migration files in prisma/migrations/
  • Applies the migration to your database
  • Regenerates Prisma Client automatically

2. Review Migration

Before committing, review the generated migration file:

prisma/migrations/xxxx_add_phone_field/migration.sql
-- AlterTable
ALTER TABLE "User" ADD COLUMN "phone" TEXT;

3. Production Deployment

For production deployments, use:

Terminal
npx prisma migrate deploy

This command:

  • Applies pending migrations to the production database
  • Does not generate new migrations
  • Does not regenerate Prisma Client (run prisma generate separately if needed)

Alternative: Push Changes (Development Only)

For rapid prototyping during development, you can push schema changes directly without creating a migration:

Terminal
npx prisma db push

Generate Prisma Client

After schema changes, regenerate the Prisma Client to update TypeScript types:

Terminal
npx prisma generate

Migration Best Practices

  1. Always use migrations for production deployments
  2. Review migration files before committing them
  3. Test migrations on a staging database first
  4. Commit migration files to version control
  5. Never edit existing migrations - create new ones instead
  6. Use descriptive migration names (e.g., add_phone_to_user)

Schema Relationships

Prisma supports defining relationships between models. For example:

prisma/schema.prisma
model User {
  id            String         @id @default(uuid())
  email         String         @unique
  organizations Organization[]
}

model Organization {
  id      String @id @default(uuid())
  name    String
  ownerId String
  owner   User   @relation(fields: [ownerId], references: [id])
}

For more information on relationships, see the Prisma relations documentation.

Creating Migrations Without Applying

To create a migration file without applying it to the database:

Terminal
npx prisma migrate dev --create-only --name migration_name

This is useful when you want to review or modify the migration SQL before applying it.