Skip to content

Nested filters are not applied to count ORM methods #6666

@xmajzel

Description

@xmajzel

Describe the bug

Em.count does not applies nested filters, when using OneToMany JOIN. Test in reproduction part should clearly show the problem.

Reproduction

import {
  BaseEntity,
  Collection,
  Entity, Filter,
  ManyToOne,
  MikroORM,
  OneToMany,
  PrimaryKey,
  DateTimeType,
  Property,
  OneToOne, Ref,
} from '@mikro-orm/sqlite';


@Entity({ abstract: true })
@Filter({
  name: 'softDelete',
  cond: {
    deletedAt: null,
  },
  default: true,
})
export abstract class BE extends BaseEntity {

  @PrimaryKey({ autoincrement: true })
  readonly id!: string;


  @Property({ type: DateTimeType, nullable: true })
  deletedAt?: Date;

}


@Entity()
class A extends BE {


  @Property()
  title!: string;

  @ManyToOne(() => B, { ref: true })
  b!: Ref<B>;

}

@Entity()
class B extends BE {

  @OneToMany(() => A, a => a.b)
  a = new Collection<A>(this);

  @OneToOne(() => C, c => c.b, {
    ref: true,
  })
  c?: Ref<C>;

}

@Entity()
class C extends BE {

  @OneToOne(() => B, { ref: true })
  b?: Ref<B>;

}

describe('GH issue XXXX', () => {

  let orm: MikroORM;

  beforeAll(async () => {
    orm = await MikroORM.init({
      dbName: ':memory:',
      entities: [A, B, C],
    });
  });

  afterAll(async () => {
    await orm.close(true);
  });

  beforeEach(async () => {
    await orm.schema.dropSchema();
    await orm.schema.createSchema();
  });

  test('findAndCount: returned rows length and count should match', async () => {


    const c1 = orm.em.create(C, {});
    const c2 = orm.em.create(C, {});

    const b1 = orm.em.create(B, { c: c1 });
    const b2 = orm.em.create(B, { c: c2 });

    const a1 = orm.em.create(A, { b: b1, title: 'test' });
    const a2 = orm.em.create(A, { b: b2, title: 'test', deletedAt: new Date() });


    await orm.em.flush();
    orm.em.clear();

    const [rows, count] = await orm.em.findAndCount(C, { b: { a: { title: 'test' } } });

    expect(rows.length).toEqual(count);
  });
});

What driver are you using?

None

MikroORM version

6.4.15

Node.js version

20

Operating system

Mac OS

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions