Skip to content

Wrong Query generated #8804

@aashisharoradev

Description

@aashisharoradev

What are you doing?

myModel.findAndCountAll({
include: [
            {
                model: model2,
                required: false
            }],
        where: {conditions},
        order: [myModel.primaryKey],
        limit: 10,
        offset: 1
});

In this case when query is generated ....ORDER BY [myModel].[primaryKey], [myModel].[primaryKey] OFFSET 0 ROWS FETCH NEXT10 ROWS ONLY;

What do you expect to happen?

expected query should be like ... ORDER BY [myModel].[primaryKey] OFFSET 0 ROWS FETCH NEXT10 ROWS ONLY;

What is actually happening?

because having order on same key two times database is throwing error.

A column has been specified more than once in the order by list. Columns in the order by list must be unique

Solution

As per my understanding if order is already specified library should not append default order.
On below line
https://github.com/sequelize/sequelize/blob/master/lib/dialects/mssql/query-generator.js#L837
condition should be something like:

if (!options.order || (options.include && orders.subQueryOrder && !orders.subQueryOrder.length)) {
        fragment += options.order && !isSubQuery ? ' ' : ' ORDER BY ';
        if (!options.order) {
          fragment +=
            this.quoteTable(options.tableAs || model.name) + '.' + this.quoteIdentifier(model.primaryKeyField);
        }
      }

Dialect: mssql

__Tested with latest release:Yes (version: 4.26.0)

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions