apps/docs/content/docs.v6/guides/implementing-schema-changes.mdx
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:
:::warning
This guide does not apply for MongoDB.
Instead of migrate dev, db push is used for MongoDB.
:::
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:
.prisma/migrations folder, including the migration_lock.toml fileschema.prisma)Source-controlling the schema.prisma file is not enough - you must include your migration history because:
prisma migrate deploy command only runs migration filesCreate a prisma.config.ts file in the root of your project with the following content:
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"),
},
});
:::note
You'll need to install the dotenv package to load environment variables. If you haven't already, install it using your package manager:
npm install dotenv
:::
To incorporate changes from collaborators:
./prisma/migrations foldernpx prisma migrate dev
Let's walk through a sample scenario with three developers sharing schema changes:
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[]
}
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 // [!code ++]
bestPacmanScore Int? // Added by you // [!code ++]
posts Post[]
}
// Added by Javier // [!code ++]
model Tag { // [!code ++]
tagName String @id // [!code ++]
tagCategory Category // [!code ++]
} // [!code ++]
Ania adds a new field:
model User {
/* ... */
favoriteColor String?
}
And generates a migration:
npx prisma migrate dev --name new-field
npx prisma generate
Javier adds a new model:
model Tag {
tagName String @id
tagCategory Category
}
And generates a migration:
npx prisma migrate dev --name new-model
npx prisma generate
The migration history now has two new migrations:
Pull the most recent changes:
Review the merged schema:
model User {
/* ... */
favoriteColor String?
bestPacmanScore Int?
}
model Tag {
tagName String @id
tagCategory Category
posts Post[]
}
Run the migrate command:
npx prisma migrate dev
npx prisma generate
This will:
Commit:
schema.prismaNow that you understand team schema management, you can:
For more information: