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

Commit cdd8807a by Mick Hansen

Merge pull request #5020 from GabeIsman/fix-attribute-scoped-count

Fix count when scope contains attributes
2 parents 9c82172c 4aff3ae7
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
- [FIXED] Apply scopes to `aggregate` [#4764](https://github.com/sequelize/sequelize/issues/4764) - [FIXED] Apply scopes to `aggregate` [#4764](https://github.com/sequelize/sequelize/issues/4764)
- [FIXED] Improved postgres enum schema handling [#4796](https://github.com/sequelize/sequelize/issues/4796) - [FIXED] Improved postgres enum schema handling [#4796](https://github.com/sequelize/sequelize/issues/4796)
- [ADDED/FIXED] Lower case `onDelete` option to allow the use of `onDelete: 'CASCADE', hooks: true`. - [ADDED/FIXED] Lower case `onDelete` option to allow the use of `onDelete: 'CASCADE', hooks: true`.
- [FIXED] Ignore attributes in `count` [#4566](https://github.com/sequelize/sequelize/issues/4566)
# 3.13.0 # 3.13.0
- [FIXED] timestamp columns are no longer undefined for associations loaded with `separate`. [#4740](https://github.com/sequelize/sequelize/issues/4740) - [FIXED] timestamp columns are no longer undefined for associations loaded with `separate`. [#4740](https://github.com/sequelize/sequelize/issues/4740)
......
...@@ -1573,6 +1573,7 @@ Model.prototype.count = function(options) { ...@@ -1573,6 +1573,7 @@ Model.prototype.count = function(options) {
options.limit = null; options.limit = null;
options.offset = null; options.offset = null;
options.order = null; options.order = null;
options.attributes = [];
return this.aggregate(col, 'count', options); return this.aggregate(col, 'count', options);
}; };
......
...@@ -22,7 +22,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -22,7 +22,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
access_level: { access_level: {
gte: 5 gte: 5
} }
} },
attributes: ['username', 'email', 'access_level']
}, },
scopes: { scopes: {
lowAccess: { lowAccess: {
......
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, Sequelize = require('../../../../index')
, expect = chai.expect
, Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () {
describe('findAndCount', function () {
beforeEach(function () {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
gte: 5
}
},
attributes: ['username', 'email', 'access_level']
},
scopes: {
lowAccess: {
where: {
access_level: {
lte: 5
}
}
},
withOrder: {
order: 'username'
}
}
});
return this.sequelize.sync({force: true}).then(function() {
var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
];
return this.ScopeMe.bulkCreate(records);
}.bind(this));
});
it('should apply defaultScope', function () {
return this.ScopeMe.findAndCount().then(function(result) {
expect(result.count).to.equal(2);
expect(result.rows.length).to.equal(2);
});
});
it('should be able to override default scope', function () {
return this.ScopeMe.findAndCount({ where: { access_level: { gt: 5 }}})
.then(function(result) {
expect(result.count).to.equal(1);
expect(result.rows.length).to.equal(1);
});
});
it('should be able to unscope', function () {
return this.ScopeMe.unscoped().findAndCount({ limit: 1 })
.then(function(result) {
expect(result.count).to.equal(4);
expect(result.rows.length).to.equal(1);
});
});
it('should be able to apply other scopes', function () {
return this.ScopeMe.scope('lowAccess').findAndCount()
.then(function(result) {
expect(result.count).to.equal(3);
});
});
it('should be able to merge scopes with where', function () {
return this.ScopeMe.scope('lowAccess')
.findAndCount({ where: { username: 'dan'}}).then(function(result) {
expect(result.count).to.equal(1);
});
});
it('should ignore the order option if it is found within the scope', function () {
return this.ScopeMe.scope('withOrder').findAndCount()
.then(function(result) {
expect(result.count).to.equal(4);
});
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!