Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

474
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda