Manish
06/15/2021, 1:11 PMmodel Post {
id Int @id @default(autoincrement())
categories Category[]
}
model Category {
id Int @id @default(autoincrement())
name String
posts Post[]
}
If I have a list of categories, like:
[{ id: 3, name: 'books'},{ id: 5, name: 'movies' }]
How can I find all posts that have atleast one of these categories?Filippo Sarzana
06/15/2021, 1:15 PM.findMany({
where: {
categories: {
some: {
name: {
in: names,
},
},
},
},
include: {
categories: {
where: {
name: {
in:names,
},
},
},
},
})
Where names is the an string[] of expected namesRyan
06/15/2021, 1:16 PMManish
06/15/2021, 1:17 PMin:names
names will be an array like: ['books', 'movies'] ?Filippo Sarzana
06/15/2021, 1:17 PMFilippo Sarzana
06/15/2021, 1:18 PMIN('...') SQL operator 👍 It’s an array of valuesManish
06/15/2021, 1:18 PMFilippo Sarzana
06/15/2021, 1:19 PMFilippo Sarzana
06/15/2021, 1:19 PMnames with idManish
06/15/2021, 1:19 PMFilippo Sarzana
06/15/2021, 1:20 PMin operator 💪Manish
06/15/2021, 1:21 PMFilippo Sarzana
06/15/2021, 1:21 PMinFilippo Sarzana
06/15/2021, 1:24 PM.findMany({
where: {
categories: {
where: {
OR: [
name: {
in: names,
},
author: {
in: authors,
},
],
},
},
},
include: {
categories: {
where: {
OR: [
name: {
in: names,
},
author: {
in: authors,
},
],
},
},
},
})
https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting#combining-operatorsManish
06/15/2021, 1:24 PMManish
06/15/2021, 1:28 PMwhere: {
published: true,
OR: [
tags: {
some: {
name: {
in: ['books','movies'],
},
},
},
author: {
some: {
id: {
in: [3,4],
},
},
},
]
}Manish
06/15/2021, 1:29 PMFilippo Sarzana
06/15/2021, 1:30 PMManish
06/15/2021, 1:31 PMManish
06/15/2021, 1:32 PMFilippo Sarzana
06/15/2021, 1:33 PM{} in each element of the arrayFilippo Sarzana
06/15/2021, 1:33 PMFilippo Sarzana
06/15/2021, 1:33 PMOR: [{tags: ...}, {author: ...}]Manish
06/15/2021, 1:34 PMManish
06/15/2021, 1:36 PMManish
06/15/2021, 1:36 PMManish
06/15/2021, 1:38 PMManish
06/15/2021, 1:38 PMFilippo Sarzana
06/15/2021, 1:38 PMsome with where for the authorManish
06/15/2021, 1:39 PMManish
06/15/2021, 1:39 PMwhere in where.OR.1.author.where for type UserRelationFilter. Did you mean is? Available args:
type UserRelationFilter {
is?: UserWhereInput | Null
isNot?: UserWhereInput | Null
}Filippo Sarzana
06/15/2021, 1:40 PMFilippo Sarzana
06/15/2021, 1:41 PMsome applies to 1-N I presume, so you should use some 1-1 relation filterFilippo Sarzana
06/15/2021, 1:41 PMinclude the author if i’m not wrongManish
06/15/2021, 1:42 PMFilippo Sarzana
06/15/2021, 1:43 PMwhere: {
author: {
id: {
in: authors,
},
},
},Filippo Sarzana
06/15/2021, 1:43 PMManish
06/15/2021, 1:48 PMwhere: {
published: true,
OR: [
{
tags: {
some: {
name: {
in: ['books','movies'],
},
},
}},
{
author: {
id: {
in: [3,4],
},
}
}
]
},
Working like a charm!Filippo Sarzana
06/15/2021, 1:48 PMManish
06/15/2021, 1:52 PM