Exclude field depends on another field

i have schema model like this
model Question {
id String @id @default(cuid())
question String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
is_anonymous Boolean

createdBy User @relation(fields: [createdById], references: [id])
createdById String
Answer Answer[]
}
model Question {
id String @id @default(cuid())
question String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
is_anonymous Boolean

createdBy User @relation(fields: [createdById], references: [id])
createdById String
Answer Answer[]
}
and i have query like this
const answer = await ctx.db.answer.findUnique({
where: { id: input.id },
include: {
createdBy: {
select: { name: true, id: true, image: true, username: true },
},
Question: {
include: {
createdBy: {
select: { name: true, id: true, image: true, username: true },
},
},
},
Answer: true,
Like: userId
? {
where: { userId },
select: { id: true },
}
: false,
_count: {
select: { Like: true, Answer: true },
},
},
});
const answer = await ctx.db.answer.findUnique({
where: { id: input.id },
include: {
createdBy: {
select: { name: true, id: true, image: true, username: true },
},
Question: {
include: {
createdBy: {
select: { name: true, id: true, image: true, username: true },
},
},
},
Answer: true,
Like: userId
? {
where: { userId },
select: { id: true },
}
: false,
_count: {
select: { Like: true, Answer: true },
},
},
});
i want to include createdBy only if the question has the flag is_anonymous otherwise i don't want to populate the createdBy object i tried to use prisma extension but i cannot achieve this with only prisma queries i had to modify the data manually so any idea how to it?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server