Skip to content

Bug Report: _count Not Working for Self-Referential Relations in Delegate Models #2452

@2234839

Description

@2234839

Description and expected behavior

Bug: When using @@delegate inheritance with self-referential relations, ZenStack's _count aggregation returns incorrect results. The count always returns 0 even when related records exist.

Expected Behavior: When using include: { _count: { select: { replies: true } } } on a model with @@delegate inheritance, the query should return the correct count of related records, matching the behavior of regular Prisma models.

The _count feature is essential for:

  • Efficiently getting record counts without loading all related data
  • Pagination UI showing total counts
  • Dashboard statistics and analytics
  • Performance optimization (avoiding loading large relation arrays)

Environment (please complete the following information)

  • ZenStack version: 3.4.4
  • Database type: SQLite (sql.js v1.14.0)
  • Node.js version: v24.3.0
  • Package manager: pnpm

Steps to Reproduce

Repository: https://github.com/2234839/zplayground/tree/bug/delegate-count-relation-bug

A minimal reproduction case is available in the above repository.

1. Create a schema with @@delegate and self-referential relation

datasource db {
    provider = 'sqlite'
    url = 'file:./dev.db'
}

enum ContentType {
    POST
    ARTICLE
    QUESTION
}

model Content {
    id   Int         @id @default(autoincrement())
    type ContentType
    @@delegate(type)
}

model Post extends Content {
    replies  Post[]  @relation("PostReplies")
    parentId Int?
    parent   Post?   @relation("PostReplies", fields: [parentId], references: [id])
}

2. Create test data and query with _count

import { schema } from './zenstack/schema';
import { ZenStackClient } from '@zenstackhq/orm';
import { SqlJsDialect } from '@zenstackhq/orm/dialects/sql.js';
import initSqlJs from 'sql.js';

const db = new ZenStackClient(schema, {
    dialect: new SqlJsDialect({ sqlJs: new SQL.Database() }),
});
await db.$pushSchema();

// Create a post with 2 replies
const post = await db.post.create({
    data: {
        replies: {
            create: [{}, {}],
        },
    },
    include: {
        replies: true,
    },
});

console.log('[post]', post);
// Output: { id: 1, type: 'Post', parentId: null, replies: [{ id: 2, ... }, { id: 3, ... }] }
// ✓ This works correctly - shows 2 replies

// Query the same post with _count
const posts_count = await db.post.findFirst({
    where: { id: post.id },
    include: {
        _count: {
            select: { replies: true },
        },
    },
});

console.log('[posts_count]', posts_count);
// Output: { id: 1, type: 'Post', parentId: null, _count: { replies: 0 } }
// ✗ BUG: Should show replies: 2, but returns 0

Actual Behavior

The _count query returns { replies: 0 } even though:

  1. The replies were successfully created in the database
  2. The same post queried with include: { replies: true } correctly returns 2 replies
  3. The data exists and is accessible through other query methods

Expected output:

{ id: 1, type: 'Post', parentId: null, _count: { replies: 2 } }

Actual output:

{ id: 1, type: 'Post', parentId: null, _count: { replies: 0 } }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions