Skip to content

_count on inherited model not working #1467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kennyboy55 opened this issue May 26, 2024 · 3 comments
Closed

_count on inherited model not working #1467

kennyboy55 opened this issue May 26, 2024 · 3 comments
Milestone

Comments

@kennyboy55
Copy link

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):

  • ZenStack version: 2.1.2
  • Prisma version: 5.13.0
  • Database type: Postgresql

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
Copy link

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

@ymc9
Copy link
Member

ymc9 commented May 29, 2024

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.

@ymc9
Copy link
Member

ymc9 commented Jan 7, 2025

Fixed in v2.11.0

@ymc9 ymc9 closed this as completed Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants