Pull Schema From Database in Prisma

Do you need to generate your schema.prisma, but your migration folder is out of sync, or have you nuked it (I know I have)?

So recently, I had a suspicion that my schema.prisma had gone out of sync, causing some issues.

And then, I wondered how to check this against my development environment.

Thankfully, Prisma makes this easy:

DB Pull command

With Prisma you can pull the schema from your connected database by running:

npx prisma db pull

This overwrites your current schema.prisma with the one it infers from your database.

If you just want to check out what it looks like, you can just print it in the console without overwriting the file with:

npx prisma db pull --print

Which will return something like this in your console:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  posts     Post[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  authorId  Int
  author    User     @relation(fields: [authorId], references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Happy coding!

PrismaOrm
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.