Skip to content

Cannot read properties of null with cursor orderBy and after #6308

Closed
@alcalyn

Description

Describe the bug

I want to use cursor, I use orderBy on a column in a relation, and use after on id, but get errors:

On my app, I have TypeError: order is not iterable at this line (order is undefined): https://github.com/mikro-orm/mikro-orm/blob/v6.4.0/packages/core/src/drivers/DatabaseDriver.ts#L256

And when reproducing it in a mikro-orm test, I get Cannot read properties of null, with both mysql and sqlite drivers

I found this similar issue, but not using cursor: #5389

I read this part of the documentation, but there is only "easy" examples https://mikro-orm.io/api/core/class/EntityRepository#findByCursor

Reproduction

import {
  Entity,
  ManyToOne,
  MikroORM,
  PrimaryKey,
  Property,
  SqliteDriver,
} from '@mikro-orm/sqlite';


@Entity()
class Metadata {

  @PrimaryKey()
  id!: number;

  @Property()
  createdAt!: Date;

}

@Entity()
class Book {

  @PrimaryKey()
  id!: number;

  @ManyToOne({
    entity: () => Metadata,
  })
  metadata!: Metadata;

}

let orm: MikroORM;

beforeAll(async () => {
  orm = await MikroORM.init({
    entities: [Metadata, Book],
    dbName: ':memory:',
    driver: SqliteDriver,
  });
  await orm.schema.refreshDatabase();
  for (let i = 0; i < 20; ++i) {
    const book = new Book();
    const metadata = new Metadata();
    book.metadata = metadata;
    const date = new Date('2024-01-01 00:00:00');
    date.setDate(date.getDate() + i);
    metadata.createdAt = date;
    orm.em.persist([book, metadata]);
  }
  await orm.em.flush();
  orm.em.clear();
});

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

test(`TypeError: Cannot read properties of null (reading 'metadata')`, async () => {
  await orm.em.findByCursor(Book, {}, {
    first: 5,
    after: {
      id: 16,
    },
    orderBy: {
      metadata: {
        createdAt: 'desc',
      },
    },
    populate: [
      'metadata',
    ],
  });
});

What driver are you using?

@mikro-orm/mysql

MikroORM version

6.4.1

Node.js version

v20.11.1

Operating system

Ubuntu

Validations

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions