Skip to main content

Using Prisma Migrate

Creating the database schema

In this guide, you'll use Prisma Migrate to create the tables in your database. Add the following Prisma data model to your Prisma schema in prisma/schema.prisma:

prisma/schema.prisma
model Post {
id BigInt @id @default(sequence())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId BigInt
}

model Profile {
id BigInt @id @default(sequence())
bio String?
user User @relation(fields: [userId], references: [id])
userId BigInt @unique
}

model User {
id BigInt @id @default(sequence())
email String @unique
name String?
posts Post[]
profile Profile?
}

To map your data model to the database schema, you need to use the prisma migrate CLI commands:

npx prisma migrate dev --name init

This command does two things:

  1. It creates a new SQL migration file for this migration
  2. It runs the SQL migration file against the database
note

generate is called under the hood by default, after running prisma migrate dev. If the prisma-client-js generator is defined in your schema, this will check if @prisma/client is installed and install it if it's missing.

Great, you now created three tables in your database with Prisma Migrate 🚀