-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Comments
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 |
anyone know what's going on? |
@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) |
To order on a field in the through table: or in my case, I had to put CourseLesson.order in back-ticks |
Could not get i had to look at the actual sql getting generated by sequelize to get this to work... and needed to do: This seems like a hack... and breakable. (im on v5) |
@nawlbergs using a literal is always going to be easily breakable yes Have you tried ordering using the association as a parameter? (source) |
@nawlbergs if |
page not found i found here https://sequelize.org/docs/v6/other-topics/sub-queries/#using-sub-queries-for-complex-ordering how about
|
The v6 docs for that page is https://sequelize.org/docs/v6/advanced-association-concepts/eager-loading/#ordering-eager-loaded-associations |
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!
The text was updated successfully, but these errors were encountered: