Skip to content

totalCount returns 1 instead of 0 when no records are found (includePageCount: true) #49

@miketester10

Description

@miketester10

Description

When using .paginate().withPages({ includePageCount: true }), the totalCount in the meta object incorrectly returns 1 when the query returns no results ([]).

The bug appears inconsistently:

  • 0 results → totalCount: 1 (wrong)
  • 1 result → totalCount: 1 (correct by chance)
  • 2+ results → correct value

This happens especially with scalar list filters like hashtags: { hasSome: [...] }.

Environment:

  • Prisma version: [6.19.2]
  • @prisma/client version: [6.19.2]
  • prisma-extension-pagination version: [0.7.6]
  • Database: [MongoDB]

Steps to Reproduce

const hashtagList = ["non-existent-hashtag"];

const where: Prisma.PostWhereInput = {
  status: PostStatus.published,
  ...(hashtagList.length ? { hashtags: { hasSome: hashtagList } } : {}),
};

const [posts, meta] = await this.prisma.client.post
  .paginate({ where })
  .withPages({
    limit: 10,
    page: 1,
    includePageCount: true,
  });

console.log("posts.length:", posts.length);     // → 0
console.log("meta.totalCount:", meta.totalCount); // → 1 (BUG!)

// Expected behavior
posts.length === 0
meta.totalCount === 0
meta.totalPages === 0
meta.isLastPage === true


// Actual behavior
meta.totalCount is forced to 1 (and sometimes meta.totalPages and isLastPage are also wrong).

// Workaround (that works perfectly)
const [posts, totalCount] = await Promise.all([
  this.prisma.client.post.findMany({ where, skip, take: limit, orderBy: { createdAt: 'desc' } }),
  this.prisma.client.post.count({ where }),
]);

No issues or discussions about this specific bug were found in the repository.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions