-
-
Notifications
You must be signed in to change notification settings - Fork 132
Open
Description
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 0Actual Behavior
The _count query returns { replies: 0 } even though:
- The replies were successfully created in the database
- The same post queried with
include: { replies: true }correctly returns 2 replies - 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 } }Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels