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
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:
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:
npx prisma migrate dev --name add_phone_fieldThis command:
- Analyzes your
schema.prismafile - Compares it with the current database state
- Generates SQL migration files in
prisma/migrations/ - Applies the migration to your database
- Regenerates Prisma Client automatically
Migration Files
Migration files are stored in prisma/migrations/ and should be
committed to version control. Each migration has a unique name and contains
the SQL statements needed to apply the changes.
2. Review Migration
Before committing, review the generated migration file:
-- AlterTable
ALTER TABLE "User" ADD COLUMN "phone" TEXT;3. Production Deployment
For production deployments, use:
npx prisma migrate deployThis command:
- Applies pending migrations to the production database
- Does not generate new migrations
- Does not regenerate Prisma Client (run
prisma generateseparately if needed)
Alternative: Push Changes (Development Only)
For rapid prototyping during development, you can push schema changes directly without creating a migration:
npx prisma db pushWarning
db push is useful for development but should not be used in
production. Always use migrations (migrate dev and
migrate deploy) for production deployments.
Generate Prisma Client
After schema changes, regenerate the Prisma Client to update TypeScript types:
npx prisma generateAutomatic Generation
prisma migrate dev automatically runs
prisma generate after applying migrations. You only need to run
it manually when using db push or after pulling schema changes.
Migration Best Practices
- Always use migrations for production deployments
- Review migration files before committing them
- Test migrations on a staging database first
- Commit migration files to version control
- Never edit existing migrations - create new ones instead
- Use descriptive migration names (e.g.,
add_phone_to_user)
Schema Relationships
Prisma supports defining relationships between models. For example:
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:
npx prisma migrate dev --create-only --name migration_nameThis is useful when you want to review or modify the migration SQL before applying it.