Prisma: relationship in findMany with where doesn't work

Issue

I’m trying to do a query with Prisma following this documentation but the accepted object doesn’t have where as an attribute.

My code:

return await prisma.post_engagement.findMany({
        select: {
            user: {
                where: {
                    enabled: true,
                    deleted_at: null,
                }
            }
        }
    });

Compilation error:

index.ts:15:17 - error TS2322: Type '{ where: { enabled: true; deleted_at: null; }; }' is not assignable to type 'boolean | userArgs'.
  Object literal may only specify known properties, and 'where' does not exist in type 'userArgs'.

15                 where: {
                   ~~~~~~~~
16                     enabled: true,
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17                     deleted_at: null,
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18                 }
   ~~~~~~~~~~~~~~~~~

  node_modules/.prisma/client/index.d.ts:68498:5
    68498     user?: boolean | userArgs
              ~~~~
    The expected type comes from property 'user' which is declared here on type 'post_engagementSelect'

Schemas:

model user {
  id             Int        @id @default(autoincrement()) @db.UnsignedInt
  email          String?    @unique(map: "user_email_unique") @db.VarChar(255)
  username       String?    @unique(map: "user_username_unique") @db.VarChar(255)
  enabled        Boolean?   @default(true)
  deleted_at     DateTime?  @db.Timestamp(0)  
}

model post_engagement {
  id             Int      @id @default(autoincrement()) @db.UnsignedInt
  user_id        Int      @db.UnsignedInt
  created_at     DateTime @default(now()) @db.Timestamp(0)
  updated_at     DateTime @default(now()) @db.Timestamp(0)
  user           user     @relation(fields: [user_id], references: [id], onUpdate: Restrict, map: "post_engagement_ibfk_2")

  @@index([user_id], map: "user_id")
}

Am I missing something?

Solution

The where parameter exists at the top level of the query options but you can still use it to specify conditions on relations.

await prisma.post_engagement.findMany({
    select: {
      user: true
    },
    where: {
      user: {
        enabled: true,
        deleted_at: null
      }
    }
  })

Answered By – Austin Crim

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.