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

How to orderby belongsToMany association using the field in the through table? #7634

Closed
itrethan opened this issue May 14, 2017 · 10 comments
Closed

Comments

@itrethan
Copy link

I have following model:
Course {
name;
...
}
Lessson {
name;
...
}
CourseLesson {
order;
...
}
Course.belongsToMany(Lesson, {through: CourseLesson , as: 'lesson'})
Lessson .belongsToMany(Course, {through: CourseLesson, as: 'course'});

In the query's order field, how could I order the lesson if I include the lesson in the query?
include: [{model: Lessson , as: 'lesson'}]
order: [{model: ???, as: ???} , order , ASC]

Thanks!

@itrethan itrethan changed the title How to orderby belongsToMany using the field in the through table? How to orderby belongsToMany association using the field in the through table? May 14, 2017
@sushantdhiman
Copy link
Contributor

http://docs.sequelizejs.com/manual/tutorial/models-usage.html#ordering-eager-loaded-associations

@itrethan
Copy link
Author

Thanks @sushantdhiman ,

But the doc only described about the nested use case, for my example,

order: [ [CourseLesson , order , ASC] ]

It doesn't seems to work, it's giving out error: 'CourseLesson ' in order / group clause is not valid association

@itrethan
Copy link
Author

anyone know what's going on?

@sushantdhiman
Copy link
Contributor

@itrethan Can you present a SSCCE which I can execute, a small, self contained (Sequelize based) code which I can quick run to reproduce problem you are facing ? For example like this #7318 (comment)

@aurbman
Copy link

aurbman commented Aug 1, 2018

To order on a field in the through table:
order: [[Sequelize.literal('CourseLesson.order'), 'ASC']]

or in my case, I had to put CourseLesson.order in back-ticks

@nawlbergs
Copy link

Could not get order: [[Sequelize.literal('CourseLesson.order'), 'ASC']] with my models

i had to look at the actual sql getting generated by sequelize to get this to work... and needed to do:
[ Sequelize.literal("`Lessson->CourseLesson`.`order`"), 'asc'],

This seems like a hack... and breakable. (im on v5)

@ephys
Copy link
Member

ephys commented Jan 6, 2022

@nawlbergs using a literal is always going to be easily breakable yes

Have you tried ordering using the association as a parameter?
Something like [Lesson.associations.CourseLesson, 'order', 'ASC'], ? (just guessing your field names)

(source)

@rhwinter
Copy link

rhwinter commented Aug 1, 2022

@nawlbergs if [ Sequelize.literal("`Lessson->CourseLesson`.`order`"), 'asc'] works, then I think that what you want to do is something like:
order: [ [{ through: {model: CourseLesson, as: 'lesson'} }, 'rank', 'ASC'] ]

@yogithesymbian
Copy link

yogithesymbian commented Nov 18, 2022

http://docs.sequelizejs.com/manual/tutorial/models-usage.html#ordering-eager-loaded-associations

page not found

i found here https://sequelize.org/docs/v6/other-topics/sub-queries/#using-sub-queries-for-complex-ordering
but its shown as raw query...

how about

const relationDatabase = [
  {
    model: user,
    paranoid: false
  },
  {
    model: verification_hm,
    separate: true
  },
  {
    model: form_tug_boat,
    paranoid: false,
    include: [
      {
        model: tug_boat,
        paranoid: false
      }
    ]
  },
...
...
....
order: [
          [Sequelize.literal('verification_hm.created_at'), type] // not work
        ]
order: [
          [Sequelize.literal('$verification_hm.created_at$'), type] // not work
        ]

@WikiRik
Copy link
Member

WikiRik commented Nov 18, 2022

The v6 docs for that page is https://sequelize.org/docs/v6/advanced-association-concepts/eager-loading/#ordering-eager-loaded-associations

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

8 participants