Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

462
Views
Order and Limit does not work simultaneously with associations in sequelize

Here, I'm trying to order an associated model and limit it to 10 records. I'm using sequelize version 5.19.1 and sequelize-cli version 5.5.1. I've tried it to order without limiting the records and it worked absolutely fine, while working with the limit it does not order the way it should work. It just limits the records to 10 which is expected while does not order those records simultaneously.

Have tried using subquery: false and separate: true options but it didn't work in this case.

Associations

surgery

surgery.hasMany(models.surgeryDoctors);

surgery.belongsTo(models.species, {
  foreignKey: 'speciesId',
  targetKey: 'id',
});

surgery.hasMany(models.surgerySpecialities);

surgery doctor

surgeryDoctors.belongsTo(models.doctors, {
  foreignKey: 'doctorId',
  targetKey: 'id',
});

surgerySpecialities.belongsTo(models.doctorSpecialities, {
  foreignKey: 'specialityId',
  targetKey: 'id',
});

Query

// here db comprises the sequelize it self.

const { rows, count } = await db.surgery.findAndCountAll({
  include: [
    {
      model: db.surgeryDoctors,
      include: [
        {
          model: db.doctors,
        },
      ],
    },
    {
      model: db.species,
    },
    {
      model: db.surgerySpecialities,
      include: [
        {
          model: db.doctorSpecialities,
        },
      ],
    },
  ],
  order: [['species','name','ASC']],
  limit: 10,
});

Is there any way to fix it or any better alternative in order to achieve this? (except raw query)

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!