diff --git a/lib/dialects/mssql/query-generator.js b/lib/dialects/mssql/query-generator.js index 7f52f89aa5ca..9a87164dfd79 100644 --- a/lib/dialects/mssql/query-generator.js +++ b/lib/dialects/mssql/query-generator.js @@ -988,6 +988,8 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { } if (options.limit || options.offset) { + // TODO: document why this is adding the primary key of the model in ORDER BY + // if options.include is set if (!options.order || options.order.length === 0 || options.include && orders.subQueryOrder.length === 0) { const tablePkFragment = `${this.quoteTable(options.tableAs || model.name)}.${this.quoteIdentifier(model.primaryKeyField)}`; if (!options.order || options.order.length === 0) { diff --git a/test/support.js b/test/support.js index f9e2fc424873..5f8337e64d6a 100644 --- a/test/support.js +++ b/test/support.js @@ -276,7 +276,12 @@ const Support = { * @returns {string} the SQL string with insignificant whitespace removed. */ minifySql(sql) { - return sql.replace(/\s+/g, ' ').trim(); + // replace all consecutive whitespaces with a single plain space character + return sql.replace(/\s+/g, ' ') + // remove space before coma + .replace(/ ,/g, ',') + // remove whitespace at start & end + .trim(); }, }; diff --git a/test/unit/sql/select.test.js b/test/unit/sql/select.test.js index e3bb62d74d96..3a30bf91be9a 100644 --- a/test/unit/sql/select.test.js +++ b/test/unit/sql/select.test.js @@ -324,7 +324,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => { FROM [users] AS [user] LEFT OUTER JOIN [post] AS [POSTS] ON [user].[id_user] = [POSTS].[user_id] ORDER BY [user].[last_name] ASC - ${sql.addLimitAndOffset({ limit: 30, offset: 10, order: [['`user`.`last_name`', 'ASC']] })}; + ${sql.addLimitAndOffset({ limit: 30, offset: 10, order: [['last_name', 'ASC']], include }, User)}; `), });