Skip to content

[Bug] Runtime error when using _count inside the include #2669

@jiashengguo

Description

@jiashengguo

Description and expected behavior

/// User model
model User {
    id    Int    @id @default(autoincrement())
    email String @unique
    posts Post[]
}

/// Post model
model Post {
    id        Int       @id @default(autoincrement())
    createdAt DateTime  @default(now())
    updatedAt DateTime  @updatedAt
    title     String
    content   String?
    published Boolean   @default(false)
    author    User      @relation(fields: [authorId], references: [id])
    authorId  Int
    comments  Comment[]
}

model Comment {
    id      Int    @id @default(autoincrement())
    content String
    post    Post   @relation(fields: [postId], references: [id])
    postId  Int
}
await db.user.findMany({
    include: {
      posts: {
        select: {
          _count: {
            select: { comments: true },
          },
        },
      },
    },
  });
/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/@zenstackhq/runtime/src/client/query-utils.ts:33
        throw new QueryError(`Field "${field}" not found in model "${model}"`);
              ^
QueryError: Field "_count" not found in model "Post"
    at requireField (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/@zenstackhq/runtime/src/client/query-utils.ts:33:15)
    at <anonymous> (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/@zenstackhq/runtime/src/client/crud/dialects/sqlite.ts:186:46)
    at Array.map (<anonymous>)
    at <anonymous> (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/@zenstackhq/runtime/src/client/crud/dialects/sqlite.ts:185:26)
    at parseSelectArg (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/kysely/dist/cjs/parser/select-parser.js:14:31)
    at SelectQueryBuilderImpl.select (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:64:144)
    at SqliteCrudDialect.buildRelationJSON (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/@zenstackhq/runtime/src/client/crud/dialects/sqlite.ts:150:19)
    at <anonymous> (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/@zenstackhq/runtime/src/client/crud/dialects/sqlite.ts:62:18)
    at parseSelectArg (/Users/jiasheng/Work/repo/v3-doc-quick-start/node_modules/kysely/dist/cjs/parser/select-parser.js:14:31)

Environment (please complete the following information):

  • ZenStack version: 3.6.4
  • Database type: sqlite
  • Node.js/Bun version: 25.2.1
  • Package manager: npm

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions