Generate and document Prisma ORM schema definitions for your database models
Generate well-structured Prisma ORM schema definitions with proper data sources, generators, models, relations, and native database types.
This skill helps you create complete Prisma schema files following Prisma ORM best practices. It generates schema definitions with:
The generated schemas follow Prisma Schema Language (PSL) conventions and include proper formatting, comments, and organization.
When the user requests a Prisma schema, follow these steps:
1. **Understand Requirements**
- Ask about the database provider (PostgreSQL, MySQL, MongoDB, SQLite, SQL Server)
- Identify the data models needed (e.g., User, Post, Comment)
- Determine field types and constraints
- Clarify relationships between models
- Ask about specific database features (indexes, native types, constraints)
2. **Check for Existing Schema**
- Look for `prisma/schema.prisma` or `schema.prisma` in the project
- If it exists, read it to understand existing models and configuration
- Offer to extend/modify existing schema or create a new one
3. **Generate Data Source Configuration**
- Add appropriate `datasource db` block with provider
- Use `env("DATABASE_URL")` for connection string (never hardcode URLs)
- Include connection pooling or other provider-specific options if needed
4. **Generate Generator Block**
- Add `generator client` block for Prisma Client
- Set custom output path if requested
- Include preview features if using beta functionality
5. **Create Data Models**
- Define each model with proper naming (PascalCase for models)
- Add id field with `@id` and `@default(autoincrement())` or `@default(auto())`
- Include timestamp fields (`createdAt`, `updatedAt`) where appropriate
- Add all required fields with correct Prisma types
- Use optional fields with `?` suffix when nullable
- Apply native database types with `@db.*` attributes (e.g., `@db.VarChar(255)`, `@db.ObjectId`)
6. **Define Relations**
- Add relation fields on both sides of relationships
- Use `@relation` attribute with `fields` and `references` for foreign keys
- Include relation names for disambiguating multiple relations between same models
- Add appropriate cascade behavior if needed
7. **Add Enums**
- Define enums for status fields, roles, or other categorical data
- Use UPPERCASE naming convention for enum values
8. **Include Indexes and Constraints**
- Add `@unique` for unique constraints
- Use `@@unique([field1, field2])` for composite unique constraints
- Add `@@index([field])` for performance optimization
- Include `@@id([field1, field2])` for composite primary keys if needed
9. **Format and Comment**
- Use `///` comments to document models and fields (these appear in generated types)
- Use `//` for internal notes not exposed in types
- Ensure proper alignment (run `prisma format` conceptually)
- Group related models together
10. **Provide Usage Instructions**
- Explain how to run `npx prisma generate` to generate Prisma Client
- Document how to run `npx prisma migrate dev` for development migrations
- Explain how to use `npx prisma studio` to explore data
- Mention `npx prisma db push` for prototyping without migrations
- Recommend VS Code Prisma extension for syntax highlighting
For a blog application with users and posts:
```prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
/// User account with authentication and profile information
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
name String?
role Role @default(USER)
posts Post[]
@@index([email])
}
/// Blog post content
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String @db.VarChar(255)
content String? @db.Text
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId Int
@@index([authorId])
@@index([published])
}
enum Role {
USER
ADMIN
MODERATOR
}
```
**One-to-many relation:**
```prisma
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}
```
**Many-to-many relation:**
```prisma
model Post {
id Int @id @default(autoincrement())
categories Category[]
}
model Category {
id Int @id @default(autoincrement())
posts Post[]
}
```
**Composite unique constraint:**
```prisma
model UserRole {
userId Int
roleId Int
@@unique([userId, roleId])
}
```
**MongoDB ObjectId:**
```prisma
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/generate-prisma-schema/raw