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

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