Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
Sequelize exclude orderBy from subQuery

Trying to use sequelize findAndCountAll method, to get items. I've to use distinct, and offset with limit due to my task.

The problem is, that in associated model i've column with array type, and i need to order parent model by that array length.

My query looks like :

 const { rows, count } = await this.repo.findAndCountAll({
      where: { someField: someValue },
      col: 'someCol',
      distinct: true,
      include: [
        {
          model: someNestedModel,
          as: 'someNestedModelAssociation',
          include: [{ model: someInnerNestedModel, as: 'someInnerNestedAssociation' }]
        }
      ],
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      //@ts-ignore
      order: this.getOrderByOptions(sortOrder, orderBy),
      limit,
      offset
    });
    
    getOrderByOptions(sortOrder, orderBy) {
    switch (orderBy) {
      case Sort_By_Array_Length:
        return Sequelize.literal('json_array_length(someModelName.someColumnWithArrayName) ASC');
      default:
        return [[orderBy, sortOrder]];
    }
  }
  

The problem is, that my order by query is used both in subQuery and mainQuery. And using it into subQuery leads to error, cz there is no such field.

If i use subQuery:false flag, it works, but then i got messed with returning results, due to problems with subQuery:false and offset&limits.

So the question is, is there a way, to exclude orderBy field from subQuery?

P.S. Models have many to many association with through table.

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda