Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
DatabaseintermediateNew

Prisma Relations

Share

Model complex relationships with Prisma (1:1, 1:n, m:n)

Works with OpenClaude

You 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.prisma file exists in the prisma/ directory
  • Confirm your database connection string is set in .env

Steps

  1. Open prisma/schema.prisma and identify your datasource (PostgreSQL, MySQL, SQLite, etc.)
  2. Define a one-to-one relationship using @relation with explicit foreign key on one model
  3. Define a one-to-many relationship where one model has @relation and the many side has a foreign key field
  4. Create a join table for many-to-many relationships, or use Prisma's implicit @relation syntax
  5. Add @unique constraint on foreign keys for one-to-one relationships to prevent duplicates
  6. Run npx prisma migrate dev --name add_relations to create migration files
  7. Use include() or select() in queries to load related records eagerly
  8. Test cascade delete behavior with onDelete: Cascade or onDelete: SetNull in 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

Quick Info

CategoryDatabase
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
prismarelationsorm

Install command:

curl -o ~/.claude/skills/prisma-relations.md https://clskills.in/skills/database/prisma-relations.md

Related Database Skills

Other Claude Code skills in the same category — free to download.

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.