Get started with Prisma Migrate

This page explains how to get started with migrating your schema in a development environment using Prisma Migrate. See Developing with Prisma Migrate for a more in-depth development workflow.

Get started with Prisma Migrate

To get started with Prisma Migrate in a development environment:

  1. Create a Prisma schema:

    schema.prisma
    1datasource db {
    2 provider = "postgresql"
    3 url = env("DATABASE_URL")
    4}
    5
    6model User {
    7 id Int @id @default(autoincrement())
    8 name String
    9 posts Post[]
    10}
    11
    12model Post {
    13 id Int @id @default(autoincrement())
    14 title String
    15 published Boolean @default(true)
    16 authorId Int
    17 author User @relation(fields: [authorId], references: [id])
    18}

    You can use native type mapping attributes in your schema to decide which exact database type to create (for example, String can map to varchar(100) or text).

    1. Create the first migration:
    $prisma migrate dev --name init
    Show migration SQL

    Your Prisma schema is now in sync with your database schema and you have initialized a migration history:

    migrations/
    └─ 20210313140442_init/
    └─ migration.sql
  2. Add additional fields to your schema:

    model User {
    id Int @id @default(autoincrement())
    jobTitle String
    name String
    posts Post[]
    }
  3. Create the second migration:

    $prisma migrate dev --name added_job_title
    Show migration SQL

    Your Prisma schema is once again in sync with your database schema, and your migration history contains two migrations:

    migrations/
    └─ 20210313140442_init/
    └─ migration.sql
    └─ 20210313140442_added_job_title/
    └─ migration.sql

You now have a migration history that you can source control and use to deploy changes to test environments and production.

Edit this page on GitHub