Skip to content

Wrong Query generated #8804

Closed
Closed

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.

Activity

  1. lerit commented on Jan 31, 2018

    @lerit

    i face same problem in mssql.default order also append to the sql:
    ............................ ORDER BY [project->supervisionOrder].[name] ASC ORDER BY [report].[reportid] OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY;
    this not happen when not set Pagination (offset and limit)
    so i think this is cause by pagination.
    except fix it!

  2. blaivs commented on Feb 7, 2018

    @blaivs

    Struggling with the same problem: Sequelize is adding a default order by primary key when you are including an association while using limit or offset, so if you add ordering by model's primary key it generates wrong sql.

    I thought this solution, just adding a check for mainQueryOrder.

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

    It passes all the tests for last release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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