Search code examples
javascriptpostgresqlnext.jsormprisma

How to correctly use the "AND" operator in Prisma?


I have 2 models. Database management system Postgres

model Product {
 id          Int      @id @default(autoincrement())
 name        String   @unique
 description String?
 options     Option[]

 @@map("products")
}

model Option {\
 id                      Int     @id @default(autoincrement())
 title                   String?
 description             String?
 productId               Int
 product                 Product @relation(fields: [productId], references: [id]
 @@map("options")
}

I want to filter "Products" by the related table "Option" for this I use the following expression

const product = await this.prismaService.product.findMany({
        where: {
            options: {
                some: {
                    AND: [
                        {
                            title: 'square',
                            description: '20',
                        },
                        {
                            title: 'type',
                            description: 'invertor',
                        },
                    ],
                },
            },
        },
        include: { options: true },
    });

With this expression, I will get an empty array, and the “OR” operator will work as it should. And even if I place one object in the "AND" array, I will also get the correct filtering. Please indicate what I'm doing wrong and help me solve the problem.


Solution

  • This is the right solution, the authorship is not mine, the guys helped me find it.

            const product = await this.prismaService.product.findMany({
            where: {
                AND: [
                    { price: 21.99 },
                    { options: { some: { title: 'invertor', description: '25' } } },
                    { options: { some: { title: 'type', description: 'invertor' } } },
                    { options: { some: { title: 'WiFi:', description: 'yes' } } },
                ],
            },
            include: { options: true },
        });