Set up Prisma ORM with schema and migrations
✓Works with OpenClaudeYou are a backend developer setting up Prisma ORM in a Node.js project. The user wants to initialize Prisma, configure the database connection, define a schema, and run migrations.
What to check first
- Run
npm list prisma @prisma/clientto verify Prisma packages aren't already installed - Check if a
.envfile exists in your project root withDATABASE_URLvariable - Confirm your database (PostgreSQL, MySQL, SQLite, etc.) is running and accessible
Steps
- Install Prisma CLI and client library with
npm install @prisma/client && npm install -D prisma - Initialize Prisma with
npx prisma init— this createsprisma/schema.prismaand.envfile - Set your
DATABASE_URLin.envwith the correct connection string for your database engine - Update the
datasource dbblock inprisma/schema.prismato match your database type (postgresql, mysql, sqlite, etc.) - Define your data models in
prisma/schema.prismausing themodelkeyword with fields and types - Run
npx prisma migrate dev --name initto create your first migration and apply it to the database - Generate the Prisma Client with
npx prisma generate(automatically runs during migrate, but useful for manual updates) - Test the connection by running
npx prisma studioto open the web UI and verify schema structure
Code
// prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
// src/index.js — Example usage after schema is set up
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
async function main() {
// Create a new user
const user = await prisma.user.create({
data: {
email: 'alice@example.com',
name: 'Alice',
},
});
// Create a post connected to the user
const post = await prisma.post.create({
data: {
title: 'Hello World',
content: 'This is my first post',
published: true,
authorId: user.id,
},
});
// Query with relations
const userWithPosts = await prisma.user.find
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Query Optimizer
Analyze and optimize slow database queries
Schema Designer
Design database schema from requirements
Seed Data Generator
Generate database seed/sample data
Index Advisor
Suggest database indexes based on query patterns
ORM Model Generator
Generate ORM models from database schema
SQL to ORM
Convert raw SQL queries to ORM syntax
Database Backup Script
Create database backup and restore scripts
Want a Database skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.