3

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

Manish Karki
  • 141
  • 1
  • 1
  • 6

1 Answers1

11

I've found the solution here https://github.com/prisma/prisma/discussions/8216#discussioncomment-992302

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' } } },
        ],
      },
})
Manish Karki
  • 141
  • 1
  • 1
  • 6