Extending the Database Schema

How to add new fields and models to the Prisma schema safely.

Last updated:

Editing the Prisma schema

Open prisma/schema.prisma and add your new fields or models. For example, adding a views counter to articles:

prisma/schema.prisma
model Article {
  id        String   @id @default(cuid())
  title     String
  slug      String   @unique
  content   String
  views     Int      @default(0)   // ← new field
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Running a migration

bash
npx prisma migrate dev --name add-article-views
npx prisma generate

This generates a SQL migration file and applies it to your development database. Commit the migration file to version control.

Warning

In production, always use npx prisma migrate deploy to apply migrations. Never run migrate dev against your production database.

Using new fields in code

After regenerating the Prisma client, TypeScript will automatically suggest the new fields in all queries:

typescript
// Increment view count
await prisma.article.update({
  where: { slug },
  data: { views: { increment: 1 } },
})

// Fetch with view count
const article = await prisma.article.findUnique({
  where: { slug },
  select: { title: true, content: true, views: true },
})