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

Commit ae9abbbe by Gabe Gorelick Committed by Sushant

fix(model): ordering by renamed sub-query attribute (#8740)

1 parent b64e01ce
......@@ -1688,6 +1688,16 @@ const QueryGenerator = {
) {
subQueryOrder.push(this.quote(order, model, '->'));
}
if (subQuery) {
// Handle case where sub-query renames attribute we want to order by,
// see https://github.com/sequelize/sequelize/issues/8739
const subQueryAttribute = options.attributes.find(a => Array.isArray(a) && a[0] === order[0] && a[1]);
if (subQueryAttribute) {
order[0] = new Utils.Col(subQueryAttribute[1]);
}
}
mainQueryOrder.push(this.quote(order, model, '->'));
}
} else if (options.order instanceof Utils.SequelizeMethod) {
......
......@@ -665,6 +665,125 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(workers[0].tasks[0].title).to.equal('homework');
});
});
// https://github.com/sequelize/sequelize/issues/8739
it('supports sorting on renamed sub-query attribute', function() {
const User = this.sequelize.define('user', {
name: {
type: Sequelize.STRING,
field: 'some_other_name'
}
});
const Project = this.sequelize.define('project', { title: Sequelize.STRING });
User.hasMany(Project);
return User.sync({ force: true })
.then(() => Project.sync({ force: true }))
.then(() => {
return User.bulkCreate([
{ name: 'a' },
{ name: 'b' },
{ name: 'c' }
]);
})
.then(() => {
return User.findAll({
order: ['name'],
limit: 2, // to force use of a sub-query
include: [Project]
});
})
.then(users => {
expect(users).to.have.lengthOf(2);
expect(users[0].name).to.equal('a');
expect(users[1].name).to.equal('b');
});
});
it('supports sorting DESC on renamed sub-query attribute', function() {
const User = this.sequelize.define('user', {
name: {
type: Sequelize.STRING,
field: 'some_other_name'
}
});
const Project = this.sequelize.define('project', { title: Sequelize.STRING });
User.hasMany(Project);
return User.sync({ force: true })
.then(() => Project.sync({ force: true }))
.then(() => {
return User.bulkCreate([
{ name: 'a' },
{ name: 'b' },
{ name: 'c' }
]);
})
.then(() => {
return User.findAll({
order: [['name', 'DESC']],
limit: 2,
include: [Project]
});
})
.then(users => {
expect(users).to.have.lengthOf(2);
expect(users[0].name).to.equal('c');
expect(users[1].name).to.equal('b');
});
});
it('supports sorting on multiple renamed sub-query attributes', function() {
const User = this.sequelize.define('user', {
name: {
type: Sequelize.STRING,
field: 'some_other_name'
},
age: {
type: Sequelize.INTEGER,
field: 'a_g_e'
}
});
const Project = this.sequelize.define('project', { title: Sequelize.STRING });
User.hasMany(Project);
return User.sync({ force: true })
.then(() => Project.sync({ force: true }))
.then(() => {
return User.bulkCreate([
{ name: 'a', age: 1 },
{ name: 'a', age: 2 },
{ name: 'b', age: 3 }
]);
})
.then(() => {
return User.findAll({
order: [['name', 'ASC'], ['age', 'DESC']],
limit: 2,
include: [Project]
});
})
.then(users => {
expect(users).to.have.lengthOf(2);
expect(users[0].name).to.equal('a');
expect(users[0].age).to.equal(2);
expect(users[1].name).to.equal('a');
expect(users[1].age).to.equal(1);
})
.then(() => {
return User.findAll({
order: [['name', 'DESC'], 'age'],
limit: 2,
include: [Project]
});
})
.then(users => {
expect(users).to.have.lengthOf(2);
expect(users[0].name).to.equal('b');
expect(users[1].name).to.equal('a');
expect(users[1].age).to.equal(1);
});
});
});
describe('hasMany with alias', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!