Skip to content

Many-to-many relations orderBy constraint does not resolve #571

@cuebit

Description

@cuebit

Description

Using orderBy constraint on nested relations does not fulfil the order criteria.

This seems to be affecting the following relationships:

Steps to reproduce

  1. Create a model with at least one many-to-many relationship.
static fields() {
   return {
      id: this.attr(null),
      roles: this.belongsToMany(Role, RoleUser, 'user_id', 'role_id')
   }
}
  1. Supply some data.
User.create({
  data: {
    id: 1,
    roles: [
      { id: 1 },
      { id: 3 },
      { id: 2 }
    ]
  }
})
  1. Retrieve the data with the relation constraint.
const user = User.query().with('roles', (query) => { query.orderBy('id') }).find(1)

Expected behaviour

The user roles should be in ascending order by id.

{
  id: 1,
  roles: [
    { id: 1 }, // Role
    { id: 2 }, // Role
    { id: 3 }  // Role
  ]
} // User

The order is actually symmetrical to the order the records, pivots et all, are created.

{
  id: 1,
  roles: [
    { id: 1 }, // Role
    { id: 3 }, // Role
    { id: 2 }  // Role
  ]
} // User

Demo

An example of a Many To Many implementation can be found here.

Versions

  • Vuex ORM: 0.35.2
  • All previous versions since orderBy feat was introduced.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingresolvedIssue have been resolved but not yet released

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions