P
Prisma•2mo ago
Werdox

How to query for is:{null} OR {something: true}

When I want to update a record in a table, for the where I need to query for a related object and I need to say if the related object does not exist (is null) or a property on the related object is something psudo code
prisma.channel.update({where: {lastMessage: { OR: [ {is: null}, {someproperty: true} ] } }, data: {...}})
prisma.channel.update({where: {lastMessage: { OR: [ {is: null}, {someproperty: true} ] } }, data: {...}})
but the OR does not allow me to put is in it...
4 Replies
Nurul
Nurul•2mo ago
Hey 👋 In your case, is lastMessage a relation field of is it a composite type? Do you mind sharing the channel model so that I can better understand your usecase?
Nurul
Nurul•2mo ago
Assuming this model:
model Channel {
id String @id @default(cuid())
name String
lastMessage Message?
}

model Message {
id String @id @default(cuid())
text String
createdAt DateTime
channel Channel @relation(fields: [channelId], references: [id])
channelId String @unique
isRead Boolean @default(false)
}
model Channel {
id String @id @default(cuid())
name String
lastMessage Message?
}

model Message {
id String @id @default(cuid())
text String
createdAt DateTime
channel Channel @relation(fields: [channelId], references: [id])
channelId String @unique
isRead Boolean @default(false)
}
This query works for me:
const result = await prisma.channel.updateMany({
where: {
OR: [
{
lastMessage: null,
},
{
lastMessage: {
isRead: true,
},
},
],
},
data: {
name: 'Updated Channel Name',
},
});
const result = await prisma.channel.updateMany({
where: {
OR: [
{
lastMessage: null,
},
{
lastMessage: {
isRead: true,
},
},
],
},
data: {
name: 'Updated Channel Name',
},
});
Werdox
WerdoxOP•2mo ago
Ohhh yea I was dumb. Thank you.
Nurul
Nurul•2mo ago
No worries! I am glad to be able to help 🙂

Did you find this page helpful?