Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default index for relations broken in 6.x #5265

Closed
5 tasks done
RobTS opened this issue Feb 21, 2024 · 1 comment
Closed
5 tasks done

Default index for relations broken in 6.x #5265

RobTS opened this issue Feb 21, 2024 · 1 comment

Comments

@RobTS
Copy link

RobTS commented Feb 21, 2024

Describe the bug

Specifying a default sort order on relations is very useful to ensure that no matter where a relation is expanded, the sort order will be "correct" without the need to specify an explicit (nested) sorting index. It also prevents unwanted behavior from accidental sorting index omissions in parts of the codebase.

Unfortunately, this seems to have broken with the new 6.x release, to the point where related code is failing due to the ORM attempting to sort the root entity by a non-existant index.

Reproduction

Sample repository here: https://github.com/RobTS/mikro-orm-bug-sample

Run yarn install, then try yarn run start. You will see that mikro-orm will attempt to resolve the List entity with ListItems, but fails with the error Trying to query by not existing property List.index. The application code does not specify any order by property, let alone index, it is erroneously taken from the relation.

The error does not occur if the index on the relation in List.entity.ts is removed, but we would lose the sorting order.
This used to work correctly in 5.9.7, you can check for yourself by checking out the working-with-5.x branch (and run yarn install to get to 5.9.7) that resolves the relation without any issues.

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.1.4

Node.js version

v20.9.0

Operating system

MacOS

Validations

@B4nan
Copy link
Member

B4nan commented Feb 21, 2024

Looks like the array form is not supported, this works fine:

@OneToMany(() => ListItem, listItem => listItem.list, {
  orderBy: { index: 'asc' }, // <== object instead of array
})
items = new Collection<ListItem>(this);

@B4nan B4nan closed this as completed in fae2302 Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants