How Do I Use "And" Operator with Multiple Query Parameters in Nested Relation When Querying in Prisma?

Its my first time trying prisma and am stuck. So I have "products" and "filters" model.

I want the following query to work. The idea is, I want to fetch the products with dynamic matching query params (name and value). The product query parameters come dynamically from the frontend.

const products = await prisma.product.findMany({
        where: {
          categoryName,
          subCategoryName,
          filters: {
            some: {
              AND: [
                {
                  name: "RAM",
                  value: "32GB",
                },
                {
                  name: "Storage",
                  value: "1TB",
                },
              ],
            },
          },
        },
        include: {
          images: true,
        },
      });

If there's only one parameter, like

{
  name:"RAM",
  value:"32GB"
}

the query returns appropriate products, but if there are more that one query params (like in the original code above), it returns empty array.

my product schema looks like this, simplified,

  name                      String      
  filters                   Filter[]

my filter schema looks like this, simplified

  name                      String      
  value                     String?
  product                   Product?    @relation(fields: [productId], references:[id])
  productId                 Int?

Thank you very much

0

1 Answer

I've found the solution here

It should be like this instead apparently.

await prisma.product.findMany({
      where: {
        AND: [
          { price: 21.99 },
          { filters: { some: { name: 'ram', value: '8GB' } } },
          { filters: { some: { name: 'storage', value: '256GB' } } },
        ],
      },
})
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest