prisma-client-api-relationspor prisma
Relation Queries. Reference when using this Prisma feature.
npx skills add https://github.com/prisma/cursor-plugin --skill prisma-client-api-relationsRelation Queries
Query and modify related records.
Include Relations
Load related records:
const user = await prisma.user.findUnique({
where: { id: 1 },
include: {
posts: true,
profile: true
}
})
Filtered include
const user = await prisma.user.findUnique({
where: { id: 1 },
include: {
posts: {
where: { published: true },
orderBy: { createdAt: 'desc' },
take: 5,
select: { id: true, title: true }
}
}
})
Nested include
const user = await prisma.user.findUnique({
where: { id: 1 },
include: {
posts: {
include: {
comments: {
include: { author: true }
}
}
}
}
})
Select Relations
const user = await prisma.user.findUnique({
where: { id: 1 },
select: {
name: true,
posts: {
select: { title: true }
}
}
})
Nested Writes
Create with relations
const user = await prisma.user.create({
data: {
email: '[email protected]',
posts: {
create: [
{ title: 'Post 1' },
{ title: 'Post 2' }
]
},
profile: {
create: { bio: 'Hello!' }
}
}
})
Create or connect
const post = await prisma.post.create({
data: {
title: 'New Post',
author: {
connectOrCreate: {
where: { email: '[email protected]' },
create: { email: '[email protected]', name: 'Alice' }
}
}
}
})
Connect existing
const post = await prisma.post.create({
data: {
title: 'New Post',
author: {
connect: { id: 1 }
}
}
})
// Shorthand for foreign key
const post = await prisma.post.create({
data: {
title: 'New Post',
authorId: 1
}
})
Update Relations
Update related records
const user = await prisma.user.update({
where: { id: 1 },
data: {
posts: {
update: {
where: { id: 1 },
data: { title: 'Updated Title' }
}
}
}
})
Update many related
const user = await prisma.user.update({
where: { id: 1 },
data: {
posts: {
updateMany: {
where: { published: false },
data: { published: true }
}
}
}
})
Upsert related
const user = await prisma.user.update({
where: { id: 1 },
data: {
profile: {
upsert: {
create: { bio: 'New bio' },
update: { bio: 'Updated bio' }
}
}
}
})
Disconnect
// 1-to-1 optional
const user = await prisma.user.update({
where: { id: 1 },
data: {
profile: { disconnect: true }
}
})
// Many-to-many
const post = await prisma.post.update({
where: { id: 1 },
data: {
tags: {
disconnect: [{ id: 1 }, { id: 2 }]
}
}
})
Delete related
const user = await prisma.user.update({
where: { id: 1 },
data: {
posts: {
delete: { id: 1 }
}
}
})
// Delete many
const user = await prisma.user.update({
where: { id: 1 },
data: {
posts: {
deleteMany: { published: false }
}
}
})
Set (replace all)
// Replace all related records
const post = await prisma.post.update({
where: { id: 1 },
data: {
tags: {
set: [{ id: 1 }, { id: 2 }]
}
}
})
Relation Filters
some
At least one matches:
const users = await prisma.user.findMany({
where: {
posts: { some: { published: true } }
}
})
every
All match:
const users = await prisma.user.findMany({
where: {
posts: { every: { published: true } }
}
})
none
None match:
const users = await prisma.user.findMany({
where: {
posts: { none: { published: true } }
}
})
is / isNot (1-to-1)
const users = await prisma.user.findMany({
where: {
profile: { is: { country: 'USA' } }
}
})
Count Relations
const users = await prisma.user.findMany({
select: {
name: true,
_count: {
select: { posts: true, followers: true }
}
}
})
// { name: 'Alice', _count: { posts: 5, followers: 100 } }
Filter counted relations
const users = await prisma.user.findMany({
select: {
name: true,
_count: {
select: {
posts: { where: { published: true } }
}
}
}
})
Mais skills de prisma
prisma-cli-migrate-status
by prisma
prisma migrate status
prisma-cli-studio
by prisma
prisma studio. Reference when using this Prisma feature.
prisma-cli-validate
by prisma
prisma validate. Reference when using this Prisma feature.
prisma-cli-db-execute
by prisma
prisma db execute. Reference when using this Prisma feature.
prisma-cli-db-pull
by prisma
prisma-cli-db-pull — an installable skill for AI agents, published by prisma/cursor-plugin.
prisma-cli-db-push
by prisma
prisma db push. Reference when using this Prisma feature.
prisma-cli-db-seed
by prisma
prisma db seed. Reference when using this Prisma feature.
prisma-cli-debug
by prisma
prisma debug. Reference when using this Prisma feature.