Hello, In the following relation: ```model Post ...
# orm-help
m
Hello, In the following relation:
Copy code
model 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?
f
It should be something like
Copy code
.findMany({
  where: {
    categories: {
      some: {
        name: {
          in: names,
        },
      },
    },
  },
  include: {
    categories: {
      where: {
        name: {
          in:names,
        },
      },
    },
  },
})
Where
names
is the an
string[]
of expected names
💯 1
r
Yeah this should work 👍
m
in:names
names will be an array like:
['books', 'movies']
?
f
yes exactly
think about the
IN('...')
SQL operator 👍 It’s an array of values
m
and similarly I can pass category ids like [3, 5] ?
f
I grab the example from https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting#filtering simply changing the filtering option according to your needs
Yep, changing
names
with
id
m
Thanks. I saw the document, but wasn’t sure if I could pass the array. Thanks so much!
f
You can by using the
in
operator 💪
m
For these posts there is another relation for the author. If I wanted: All posts that 1) either have one of these two categories, OR 2) have one of these two authors. How can I combine this?
f
this is the type expected by
in
🙏 1
Copy code
.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-operators
m
Ok, I’m getting it. I’ll try in my case.
🙌 1
Copy code
where: { 
      published: true,
      OR: [
        tags: {
          some: {
            name: {
              in: ['books','movies'],
            },
          },
        },
        author: {
          some: {
            id: {
              in: [3,4],
            },
          },
        },
      ]
    }
This is showing an error. But this is what I want. Where the post either has these tags, or has these authors.
f
which error?
m
It says ‘,’ expected
f
You are missing the
{}
in each element of the array
my fault when copy/pasting in rush
Copy code
OR: [{tags: ...}, {author: ...}]
m
Oh, sorry. I got it.
Actually the query is failing at author. Post to Author is a 1-1 relation. So, each post can have only one author.
Does this change the syntax for get posts where the author is either one of the two?
The tags part worked magic.
But the author part fails.
f
try changing
some
with
where
for the author
m
Even where gives error.
Unknown arg
where
in where.OR.1.author.where for type UserRelationFilter. Did you mean
is
? Available args: type UserRelationFilter {  is?: UserWhereInput | Null  isNot?: UserWhereInput | Null }
f
I don’t have by hand your configuration, but if you follow typescript it will be easy 👍
some
applies to 1-N I presume, so you should use some 1-1 relation filter
You should also
include
the author if i’m not wrong
m
Ok thanks, let me check this. If I’m not able to resolve, I’ll post a simpler problem focusing just on this problem of 1:1
f
Copy code
where: {
  author: {
    id: {
      in: authors,
    },
  },
},
m
Perfect. It worked! Here is the final query:
Copy code
where: { 
      published: true,
      OR: [
            {
              tags: {
              some: {
                name: {
                  in: ['books','movies'],
                },
              },
            }},
            {
              author: {
                id: {
                  in: [3,4],
                },
              }
            }
          ]
    },
Working like a charm!
f
Glad it worked 🚀
m
Thank you so much!
prisma rainbow 1