zenstackhq / zenstack

Fullstack TypeScript toolkit enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
1.86k stars 80 forks source link

_count on inherited model not working #1467

Open kennyboy55 opened 1 month ago

kennyboy55 commented 1 month ago

Description and expected behavior I have a zmodel with inheritance. The base has a one to many relation. When quering the child, I am trying to use _count. However, it tries to find the relation in the child instead of the parent.

Environment (please complete the following information):

Additional context The model

model Drink {
  id                Int   @id @default(autoincrement())
  slug              String @unique

  manufacturer_id   Int
  manufacturer      Manufacturer @relation(fields: [manufacturer_id], references: [id])

  type              DrinkType

  name              String @unique
  description       String
  abv               Float
  image             String?
  link              String?

  gluten            Boolean
  lactose           Boolean
  organic           Boolean

  containers        Container[]

  @@delegate(type)

  @@allow('read', true)
  @@allow('all', auth().type == Admin)
}

model Beer extends Drink {
  style_id         Int
  style            BeerStyle @relation(fields: [style_id], references: [id])

  ibu              Float?

  glass            Boolean @default(false)

  @@allow('read', true)
  @@allow('all', auth().type == Admin)
}

The code

const beers = await dbe.beer.findMany({select: {id: true, name: true, _count: {select: {containers: true}}},orderBy: {name: "asc"}});
  return json({beers});

The error

Error calling enhanced Prisma method `beer.findMany`: 
Invalid `prisma.beer.findMany()` invocation:

Unknown nested field '_count' for operation findManyBeer does not match any query.
    at loader (/home/keewijk/projects/beer-inventory/app/routes/admin/edit/beer.tsx:14:32),
    at async Object.callRouteLoader (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/server-runtime/dist/data.js:62:16),
    at async /home/keewijk/projects/beer-inventory/node_modules/@remix-run/router/dist/router.cjs.js:4229:21,
    at async callLoaderOrAction (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/router/dist/router.cjs.js:4294:16),
    at async Promise.all (index 2),
    at async callDataStrategyImpl (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/router/dist/router.cjs.js:4169:17),
    at async callDataStrategy (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/router/dist/router.cjs.js:3702:19),
    at async loadRouteData (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/router/dist/router.cjs.js:3677:19),
    at async queryImpl (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/router/dist/router.cjs.js:3522:20),
    at async Object.query (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/router/dist/router.cjs.js:3416:18),
    at async handleDocumentRequest (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/server-runtime/dist/server.js:222:15),
    at async requestHandler (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/server-runtime/dist/server.js:141:18),
    at async nodeHandler (/home/keewijk/projects/beer-inventory/node_modules/@remix-run/dev/dist/vite/plugin.js:844:27),
    at async /home/keewijk/projects/beer-inventory/node_modules/@remix-run/dev/dist/vite/plugin.js:847:15 {
  name: 'PrismaClientUnknownRequestError',
  clientVersion: '5.13.0',
  internalStack: 'Error calling enhanced Prisma method `beer.findMany`: \n' +
    'Invalid `prisma.beer.findMany()` invocation:\n' +
    '\n' +
    '\n' +
    "Unknown nested field '_count' for operation findManyBeer does not match any query.\n" +
    '    at Generator.next (<anonymous>),\n' +
    '    at fulfilled (/home/keewijk/projects/beer-inventory/node_modules/@zenstackhq/runtime/enhancements/proxy.js:6:58)'
}
max-petrenko commented 1 month ago

Hi. Unfortunately, docs are explicitly saying that you cannot use count when filtering by the properties of the base model.

ymc9 commented 1 month ago

Aggregating with "sum", etc. doesn't work with fields from delegate base because Prisma doesn't allow using a relation field to aggregate. However, "_count" seems to be a special one and it may actually work ...

I'll check for details and comment back soon.