Model complex relationships with Prisma (1:1, 1:n, m:n)
✓Works with OpenClaudeYou are a Prisma ORM expert. The user wants to model complex relationships (one-to-one, one-to-many, many-to-many) in Prisma schema and understand how to query and manage them.
What to check first
- Verify Prisma is installed:
npx prisma --version - Check your
schema.prismafile exists in theprisma/directory - Confirm your database connection string is set in
.env
Steps
- Open
prisma/schema.prismaand identify your datasource (PostgreSQL, MySQL, SQLite, etc.) - Define a one-to-one relationship using
@relationwith explicit foreign key on one model - Define a one-to-many relationship where one model has
@relationand the many side has a foreign key field - Create a join table for many-to-many relationships, or use Prisma's implicit
@relationsyntax - Add
@uniqueconstraint on foreign keys for one-to-one relationships to prevent duplicates - Run
npx prisma migrate dev --name add_relationsto create migration files - Use
include()orselect()in queries to load related records eagerly - Test cascade delete behavior with
onDelete: CascadeoronDelete: SetNullin relations
Code
// One-to-One: User has one Profile
model User {
id Int @id @default(autoincrement())
email String @unique
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
bio String?
userId Int @unique
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
// One-to-Many: Author has many Posts
model Author {
id Int @id @default(autoincrement())
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
authorId Int
author Author @relation(fields: [authorId], references: [id], onDelete: Cascade)
}
// Many-to-Many: Students and Courses (explicit join table)
model Student {
id Int @id @default(autoincrement())
name String
enrollments Enrollment[]
}
model Course {
id Int @id @default(autoincrement())
title String
enrollments Enrollment[]
}
model Enrollment {
id Int @id @default(autoincrement())
studentId Int
courseId Int
student Student @relation(fields: [studentId], references: [id], onDelete: Cascade)
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
@@unique([studentId, courseId
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.