Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

Schema management in teams

When working in a team, managing database schema changes can be challenging. This guide shows you how to effectively collaborate on schema changes using Prisma Migrate, ensuring that all team members can safely contribute to and incorporate schema changes.

Before starting this guide, make sure you have:

  • Node.js installed (version 20 or higher)
  • A Prisma project set up with migrations
  • A relational database (PostgreSQL, MySQL, SQLite, SQL Server, etc.)
  • Basic understanding of Git
  • Basic familiarity with Prisma Migrate

Migrations are applied in the same order as they were created. The creation date is part of the migration subfolder name - for example, 20210316081837-updated-fields was created on 2021-03-16-08:18:37.

You should commit the following files to source control:

  • The contents of the prisma/migrations folder, including the migration_lock.toml file
  • The Prisma Schema (schema.prisma)

Source-controlling the schema.prisma file is not enough - you must include your migration history because:

  • Customized migrations contain information that cannot be represented in the Prisma schema
  • The prisma migrate deploy command only runs migration files

Create a prisma.config.ts file in the root of your project with the following content:

title="prisma.config.ts"
import "dotenv/config";

import { defineConfig, env } from "prisma/config";

export default defineConfig({

  schema: "prisma/schema.prisma",

  migrations: {

    path: "prisma/migrations",

  },

  datasource: {

    url: env("DATABASE_URL"),

  },

});

To incorporate changes from collaborators:

  1. Pull the changed Prisma schema and ./prisma/migrations folder
  2. Run the migrate command:
title="bun"
bunx prisma migrate dev
pnpm
pnpm prisma migrate dev --name new-field
yarn
yarn prisma migrate dev --name new-field
npm
npx prisma migrate dev --name new-field

Consider a sample scenario with three developers sharing schema changes:

title="Before"
model Post {

  id        Int     @id @default(autoincrement())

  title     String

  content   String?

  published Boolean @default(false)

  author    User?   @relation(fields: [authorId], references: [id])

  authorId  Int?

}

model User {

  id    Int     @id @default(autoincrement())

  email String  @unique

  name  String?

  posts Post[]

}
After
model Post {  id        Int     @id @default(autoincrement())  title     String  content   String?  published Boolean @default(false)  author    User?   @relation(fields: [authorId], references: [id])  authorId  Int?}model User {  id              Int     @id @default(autoincrement())  email           String  @unique  name            String?  favoriteColor   String? // Added by Ania  bestPacmanScore Int? // Added by you  posts           Post[]}// Added by Javiermodel Tag {  tagName     String   @id  tagCategory Category}

Ania adds a new field:

model User {

  /* ... */

  favoriteColor String?

}

And generates a migration:

title="bun"
bunx prisma migrate dev --name new-field
pnpm
pnpm prisma generate
yarn
yarn prisma generate
npm
npx prisma generate
title="bun"
bunx prisma generate
pnpm
pnpm prisma migrate dev --name new-model
yarn
yarn prisma migrate dev --name new-model
npm
npx prisma migrate dev --name new-model

Javier adds a new model:

model Tag {

  tagName     String   @id

  tagCategory Category

}

And generates a migration:

title="bun"
bunx prisma migrate dev --name new-model
pnpm
pnpm prisma generate
yarn
yarn prisma generate
npm
npx prisma generate
title="bun"
bunx prisma generate
pnpm
pnpm prisma migrate dev
yarn
yarn prisma migrate dev
npm
npx prisma migrate dev

The migration history now has two new migrations:

A diagram showing changes by two separate developers converging in a single migration history.
  1. Pull the most recent changes:

    • Two new migrations
    • Updated schema file
  2. Review the merged schema:

model User {

  /* ... */

  favoriteColor   String?

  bestPacmanScore Int?

}

model Tag {

  tagName     String   @id

  tagCategory Category

  posts       Post[]

}

Run the migrate command:

bunx prisma migrate dev
Bash
pnpm prisma generate
Bash
yarn prisma generate
Bash
npx prisma generate

This will:

  1. Apply your team's migrations
  2. Create a new migration for your changes
  3. Apply your new migration
bunx prisma generate

This will:

  1. Apply your team's migrations
  2. Create a new migration for your changes
  3. Apply your new migration

Commit:

  • The merged schema.prisma
  • Your new migration file

Now that you understand team schema management, you can:

For more information:

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu