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

Commit 6347f7d5 by u9r52sld Committed by Sushant

fix(scope): incorrect query generated when sequelize.fn used with scopes(#9730)

1 parent d5e9f124
......@@ -1532,7 +1532,14 @@ class Model {
if (scope) {
_.assignWith(self._scope, scope, (objectValue, sourceValue, key) => {
if (key === 'where') {
return Array.isArray(sourceValue) ? sourceValue : Object.assign(objectValue || {}, sourceValue);
if (Array.isArray(sourceValue)) {
return sourceValue;
} else {
if (sourceValue instanceof Utils.SequelizeMethod) {
sourceValue = { [Op.and]: sourceValue };
}
return Object.assign(objectValue || {}, sourceValue);
}
} else if (['attributes', 'include', 'group'].indexOf(key) >= 0 && Array.isArray(objectValue) && Array.isArray(sourceValue)) {
return objectValue.concat(sourceValue);
}
......
......@@ -50,6 +50,9 @@ describe(Support.getTestDialectTeaser('Model'), () => {
},
access_level: 5
}
},
like_t: {
where: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('username')), 'LIKE', '%t%')
}
}
});
......@@ -108,5 +111,12 @@ describe(Support.getTestDialectTeaser('Model'), () => {
return this.ScopeMe.scope('issue8473').findAll();
});
});
it('should not throw error with sequelize.where', function() {
return this.ScopeMe.scope('like_t').findAll()
.then(records => {
expect(records).to.have.length(2);
});
});
});
});
......@@ -2,6 +2,8 @@
const chai = require('chai'),
expect = chai.expect,
Sequelize = require(__dirname + '/../../../index'),
Op = Sequelize.Op,
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
current = Support.sequelize;
......@@ -28,6 +30,9 @@ describe(Support.getTestDialectTeaser('Model'), () => {
something: false
}
},
sequelize_where: {
where: Sequelize.where()
},
users: {
include: [
{ model: User }
......@@ -135,10 +140,11 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
it('should be able to merge scopes', () => {
expect(Company.scope('somethingTrue', 'somethingFalse')._scope).to.deep.equal({
expect(Company.scope('somethingTrue', 'somethingFalse', 'sequelize_where')._scope).to.deep.equal({
where: {
something: false,
somethingElse: 42
somethingElse: 42,
[Op.and]: Sequelize.where()
},
limit: 5
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!