Skip to content

Query Builder getManyAndCount() returns incorrect data and count when used in conjunction with pagination + join + a condition on the joined column #11744

Open

Description

Issue description

query builder getManyAndCount() returns incorrect results in typeorm 0.3.26+

Expected Behavior

getManyAndCount() should return the same count as getCount()

Actual Behavior

The count will be inaccurate and the page may be truncated

Steps to reproduce

This bug requires a couple conditions for it to appear:

  • Use query builder with pagination (take) and a join to another table.
  • At least one of the returned entities on the page should have 0 related entries in the joined table.
  • At least one of the returned entities on the page should have 2+ related entries in the joined table.

Minimal example

@Entity()
export class Sport {
  @PrimaryGeneratedColumn()
  id!: number;
  
  @Column()
  name?: string;

  @OneToMany(
    () => Player,
    (player) => player.sport,
  )
  players!: Players[];
}

@Entity()
export class Player {
  @PrimaryGeneratedColumn()
  id!: number;
  
  @Column()
  name?: string;

  @Column()
  sportId?: number;

  @ManyToOne(() => Sport, (sport) => sport.players)
  @JoinColumn({ referencedColumnName: 'id', name: 'sportId' })
  sport!: Sport;
}

Sports table entites:

id name
1 soccer
2 tennis

Players table entities:

id name sportId
1 Alice 1
2 Bob 1

query:

const results = await this.sportsRepo.createQueryBuilder('sports')
      .leftJoinAndSelect('sports.players', 'players')
      .addOrderBy('players.name', 'DESC')
      .take(2)
      .getManyAndCount();
return results[1];

On typeorm 0.3.26+, this returns 1, which is incorrect.
On typeorm 0.3.25, this returns 2, which is correct.

My Environment

Dependency Version
Node.js version 20.x
Typescript version 5.x
TypeORM version 0.3.26+

This bug is not present on typeorm 0.3.25

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Activity

  1. sgarner commented on Oct 30, 2025

    @sgarner
    Collaborator

    TypeORM v0.3.27 contained a fix for incorrect counts: #11634

    Your issue looks different. But can you confirm it is still present in v0.3.27?

  2. JoshuaEwing-Semanticbits commented on Oct 30, 2025

    @JoshuaEwing-Semanticbits
    Author

    Yes, this is a separate issue from #11634 and is still present in v0.3.27.

  3. added
    regression
    and removed on Nov 9, 2025
  4. added a commit that references this issue on Sep 20, 2026
    acf7c9d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugregression

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions