不要怂,就是干,撸起袖子干!

Commit 4d75caf4 by Sushant Committed by GitHub

fix(mssql): duplicate order in FETCH/NEXT queries (#12257)

1 parent 9a95e727
...@@ -917,8 +917,18 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { ...@@ -917,8 +917,18 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
if (options.limit || options.offset) { if (options.limit || options.offset) {
if (!options.order || options.include && !orders.subQueryOrder.length) { if (!options.order || options.include && !orders.subQueryOrder.length) {
const tablePkFragment = `${this.quoteTable(options.tableAs || model.name)}.${this.quoteIdentifier(model.primaryKeyField)}`;
if (!options.order) {
fragment += ` ORDER BY ${tablePkFragment}`;
} else {
const orderFieldNames = _.map(options.order, order => order[0]);
const primaryKeyFieldAlreadyPresent = _.includes(orderFieldNames, model.primaryKeyField);
if (!primaryKeyFieldAlreadyPresent) {
fragment += options.order && !isSubQuery ? ', ' : ' ORDER BY '; fragment += options.order && !isSubQuery ? ', ' : ' ORDER BY ';
fragment += `${this.quoteTable(options.tableAs || model.name)}.${this.quoteIdentifier(model.primaryKeyField)}`; fragment += tablePkFragment;
}
}
} }
if (options.offset || options.limit) { if (options.offset || options.limit) {
......
...@@ -8,7 +8,7 @@ const chai = require('chai'), ...@@ -8,7 +8,7 @@ const chai = require('chai'),
dialect = Support.getTestDialect(); dialect = Support.getTestDialect();
if (dialect.match(/^mssql/)) { if (dialect.match(/^mssql/)) {
describe('[MSSQL Specific] Regressions', () => { describe(Support.getTestDialectTeaser('Regressions'), () => {
it('does not duplicate columns in ORDER BY statement, #9008', async function() { it('does not duplicate columns in ORDER BY statement, #9008', async function() {
const LoginLog = this.sequelize.define('LoginLog', { const LoginLog = this.sequelize.define('LoginLog', {
ID: { ID: {
...@@ -80,7 +80,54 @@ if (dialect.match(/^mssql/)) { ...@@ -80,7 +80,54 @@ if (dialect.match(/^mssql/)) {
expect(logs).to.have.length(2); expect(logs).to.have.length(2);
expect(logs[0].User.get('UserName')).to.equal('Shaktimaan'); expect(logs[0].User.get('UserName')).to.equal('Shaktimaan');
expect(logs[1].User.get('UserName')).to.equal('Aryamaan'); expect(logs[1].User.get('UserName')).to.equal('Aryamaan');
// #11258 and similar
const otherLogs = await LoginLog.findAll({
include: [
{
model: User,
where: {
UserName: {
[Op.like]: '%maan%'
}
}
}
],
order: [['id', 'DESC']],
offset: 0,
limit: 10
}); });
expect(otherLogs).to.have.length(2);
expect(otherLogs[0].User.get('UserName')).to.equal('Aryamaan');
expect(otherLogs[1].User.get('UserName')).to.equal('Shaktimaan');
// Separate queries can apply order freely
const separateUsers = await User.findAll({
include: [
{
model: LoginLog,
separate: true,
order: [
'id'
]
}
],
where: {
UserName: {
[Op.like]: '%maan%'
}
},
order: ['UserName', ['UserID', 'DESC']],
offset: 0,
limit: 10
});
expect(separateUsers).to.have.length(2);
expect(separateUsers[0].get('UserName')).to.equal('Aryamaan');
expect(separateUsers[0].get('LoginLogs')).to.have.length(1);
expect(separateUsers[1].get('UserName')).to.equal('Shaktimaan');
expect(separateUsers[1].get('LoginLogs')).to.have.length(1);
}); });
it('sets the varchar(max) length correctly on describeTable', async function() { it('sets the varchar(max) length correctly on describeTable', async function() {
...@@ -148,4 +195,5 @@ if (dialect.match(/^mssql/)) { ...@@ -148,4 +195,5 @@ if (dialect.match(/^mssql/)) {
const record = await BooleanTable.findOne(); const record = await BooleanTable.findOne();
expect(record.status).to.equals(value); expect(record.status).to.equals(value);
}); });
});
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!