Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing a bug with sequelize.where in model scope #9730

Merged
merged 6 commits into from Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/model.js
Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions test/integration/model/scope.test.js
Expand Up @@ -50,6 +50,9 @@ describe(Support.getTestDialectTeaser('Model'), () => {
},
access_level: 5
}
},
like_t: {
where: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('username')), 'LIKE', '%t%')
}
}
});
Expand Down Expand Up @@ -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);
});
});
});
});
10 changes: 8 additions & 2 deletions test/unit/model/scope.test.js
Expand Up @@ -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;
Expand All @@ -28,6 +30,9 @@ describe(Support.getTestDialectTeaser('Model'), () => {
something: false
}
},
sequelize_where: {
where: Sequelize.where()
},
users: {
include: [
{ model: User }
Expand Down Expand Up @@ -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
});
Expand Down