prisma-relations
Prisma relation modeling patterns
What this skill does
# Prisma Relations Skill
Patterns for modeling relationships in Prisma.
## One-to-One Relations
### Basic One-to-One
```prisma
model User {
id String @id @default(uuid())
email String @unique
profile Profile?
}
model Profile {
id String @id @default(uuid())
bio String?
avatar String?
userId String @unique
user User @relation(fields: [userId], references: [id])
}
```
### Queries
```typescript
// Create with relation
const user = await prisma.user.create({
data: {
email: '[email protected]',
profile: {
create: { bio: 'Hello world' },
},
},
include: { profile: true },
})
// Update nested
await prisma.user.update({
where: { id: userId },
data: {
profile: {
upsert: {
create: { bio: 'New bio' },
update: { bio: 'Updated bio' },
},
},
},
})
// Delete relation
await prisma.user.update({
where: { id: userId },
data: {
profile: { delete: true },
},
})
```
## One-to-Many Relations
### Basic One-to-Many
```prisma
model User {
id String @id @default(uuid())
posts Post[]
}
model Post {
id String @id @default(uuid())
title String
authorId String
author User @relation(fields: [authorId], references: [id])
@@index([authorId])
}
```
### Queries
```typescript
// Create with multiple related
const user = await prisma.user.create({
data: {
email: '[email protected]',
posts: {
create: [
{ title: 'Post 1' },
{ title: 'Post 2' },
],
},
},
include: { posts: true },
})
// Add to existing relation
await prisma.post.create({
data: {
title: 'New Post',
author: {
connect: { id: userId },
},
},
})
// Update relation
await prisma.user.update({
where: { id: userId },
data: {
posts: {
create: { title: 'Another Post' },
updateMany: {
where: { published: false },
data: { published: true },
},
},
},
})
// Filter by relation
const users = await prisma.user.findMany({
where: {
posts: {
some: { published: true },
},
},
})
```
## Many-to-Many Relations
### Implicit Many-to-Many
```prisma
model Post {
id String @id @default(uuid())
title String
categories Category[]
}
model Category {
id String @id @default(uuid())
name String @unique
posts Post[]
}
```
```typescript
// Create with connections
const post = await prisma.post.create({
data: {
title: 'My Post',
categories: {
connect: [
{ id: 'category-1' },
{ id: 'category-2' },
],
},
},
})
// Connect or create
const post = await prisma.post.create({
data: {
title: 'My Post',
categories: {
connectOrCreate: [
{
where: { name: 'Tech' },
create: { name: 'Tech' },
},
],
},
},
})
// Set (replace all)
await prisma.post.update({
where: { id: postId },
data: {
categories: {
set: [{ id: 'category-1' }],
},
},
})
// Disconnect
await prisma.post.update({
where: { id: postId },
data: {
categories: {
disconnect: [{ id: 'category-2' }],
},
},
})
```
### Explicit Many-to-Many (Join Table)
```prisma
model Post {
id String @id @default(uuid())
tags PostTag[]
}
model Tag {
id String @id @default(uuid())
name String @unique
posts PostTag[]
}
model PostTag {
postId String
tagId String
createdAt DateTime @default(now())
createdBy String?
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
tag Tag @relation(fields: [tagId], references: [id], onDelete: Cascade)
@@id([postId, tagId])
}
```
```typescript
// Create with join table data
await prisma.postTag.create({
data: {
post: { connect: { id: postId } },
tag: { connect: { id: tagId } },
createdBy: userId,
},
})
// Query with join table data
const posts = await prisma.post.findMany({
include: {
tags: {
include: { tag: true },
},
},
})
```
## Self-Relations
### Parent-Child (Tree)
```prisma
model Category {
id String @id @default(uuid())
name String
parentId String?
parent Category? @relation("CategoryTree", fields: [parentId], references: [id])
children Category[] @relation("CategoryTree")
}
```
```typescript
// Create hierarchy
const parent = await prisma.category.create({
data: {
name: 'Electronics',
children: {
create: [
{ name: 'Phones' },
{ name: 'Laptops' },
],
},
},
include: { children: true },
})
// Get with nested children (recursive requires raw query or multiple queries)
const category = await prisma.category.findUnique({
where: { id: categoryId },
include: {
children: {
include: {
children: true,
},
},
},
})
```
### Following/Followers
```prisma
model User {
id String @id @default(uuid())
name String
followedBy User[] @relation("UserFollows")
following User[] @relation("UserFollows")
}
```
```typescript
// Follow user
await prisma.user.update({
where: { id: currentUserId },
data: {
following: {
connect: { id: targetUserId },
},
},
})
// Get followers
const user = await prisma.user.findUnique({
where: { id: userId },
include: {
followedBy: { select: { id: true, name: true } },
following: { select: { id: true, name: true } },
},
})
```
## Cascade Operations
```prisma
model User {
id String @id @default(uuid())
posts Post[]
}
model Post {
id String @id @default(uuid())
authorId String
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
comments Comment[]
}
model Comment {
id String @id @default(uuid())
postId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
}
```
```typescript
// Deleting user cascades to posts, which cascade to comments
await prisma.user.delete({
where: { id: userId },
})
```
## Relation Loading Strategies
```typescript
// Include (eager load)
const user = await prisma.user.findUnique({
where: { id: userId },
include: { posts: true },
})
// Select specific relation fields
const user = await prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
posts: { select: { id: true, title: true } },
},
})
// Separate queries (for large relations)
const user = await prisma.user.findUnique({ where: { id: userId } })
const posts = await prisma.post.findMany({ where: { authorId: userId } })
```
## Integration
Used by:
- `database-developer` agent
- `fullstack-developer` agent
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.