sequelize / sequelize

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://sequelize.org/
MIT License
29.52k stars 4.27k forks source link

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

Closed itrethan closed 7 years ago

itrethan commented 7 years ago

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!

sushantdhiman commented 7 years ago

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

itrethan commented 7 years ago

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 commented 7 years ago

anyone know what's going on?

sushantdhiman commented 7 years ago

@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 https://github.com/sequelize/sequelize/issues/7318#issuecomment-284136530

aurbman commented 6 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

@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 commented 1 year ago

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 commented 1 year ago

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