Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 4244ac8d
authored
Nov 27, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(model): support custom fields in aggregate methods, closes #4935
1 parent
bdb1c731
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
6 deletions
changelog.md
lib/model.js
test/integration/model.test.js
changelog.md
View file @
4244ac8
# Next]
-
[
FIXED
]
Model.aggregate methods now support attributes and where conditions with fields.
[
#4935
](
https://github.com/sequelize/sequelize/issues/4935
)
# 3.14.1
-
[
FIXED
]
Issue with transaction options leaking and certain queries running outside of the transaction connection.
...
...
lib/model.js
View file @
4244ac8
...
...
@@ -1504,20 +1504,23 @@ Model.prototype.find = Model.prototype.findOne;
*
* @return {Promise<options.dataType|object>} Returns the aggregate result cast to `options.dataType`, unless `options.plain` is false, in which case the complete data result is returned.
*/
Model
.
prototype
.
aggregate
=
function
(
field
,
aggregateFunction
,
options
)
{
Model
.
prototype
.
aggregate
=
function
(
attribute
,
aggregateFunction
,
options
)
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{});
conformOptions
(
options
,
this
);
Model
.
$injectScope
(
this
.
$scope
,
options
);
var
aggregateColumn
=
this
.
sequelize
.
col
(
field
);
var
attrOptions
=
this
.
rawAttributes
[
attribute
]
,
field
=
attrOptions
&&
attrOptions
.
field
||
attribute
,
aggregateColumn
=
this
.
sequelize
.
col
(
field
);
if
(
options
.
distinct
)
{
aggregateColumn
=
this
.
sequelize
.
fn
(
'DISTINCT'
,
aggregateColumn
);
}
options
.
attributes
.
push
([
this
.
sequelize
.
fn
(
aggregateFunction
,
aggregateColumn
),
aggregateFunction
]);
if
(
!
options
.
dataType
)
{
if
(
this
.
rawAttributes
[
field
]
)
{
options
.
dataType
=
this
.
rawAttributes
[
field
]
.
type
;
if
(
attrOptions
)
{
options
.
dataType
=
attrOptions
.
type
;
}
else
{
// Use FLOAT as fallback
options
.
dataType
=
new
DataTypes
.
FLOAT
();
...
...
@@ -1526,6 +1529,7 @@ Model.prototype.aggregate = function(field, aggregateFunction, options) {
options
.
dataType
=
this
.
sequelize
.
normalizeDataType
(
options
.
dataType
);
}
Utils
.
mapOptionFieldNames
(
options
,
this
);
options
=
paranoidClause
(
this
,
options
);
return
this
.
QueryInterface
.
rawSelect
(
this
.
getTableName
(
options
),
options
,
aggregateFunction
,
this
);
...
...
test/integration/model.test.js
View file @
4244ac8
...
...
@@ -11,6 +11,7 @@ var chai = require('chai')
,
sinon
=
require
(
'sinon'
)
,
_
=
require
(
'lodash'
)
,
moment
=
require
(
'moment'
)
,
Promise
=
require
(
'bluebird'
)
,
current
=
Support
.
sequelize
;
describe
(
Support
.
getTestDialectTeaser
(
'Model'
),
function
()
{
...
...
@@ -1997,9 +1998,23 @@ describe(Support.getTestDialectTeaser('Model'), function() {
value
:
Sequelize
.
DECIMAL
(
10
,
3
)
});
return
this
.
UserWithAge
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
UserWithDec
.
sync
({
force
:
true
});
this
.
UserWithFields
=
this
.
sequelize
.
define
(
'UserWithFields'
,
{
age
:
{
type
:
Sequelize
.
INTEGER
,
field
:
'user_age'
},
order
:
Sequelize
.
INTEGER
,
gender
:
{
type
:
Sequelize
.
ENUM
(
'male'
,
'female'
),
field
:
'male_female'
}
});
return
Promise
.
join
(
this
.
UserWithAge
.
sync
({
force
:
true
}),
this
.
UserWithDec
.
sync
({
force
:
true
}),
this
.
UserWithFields
.
sync
({
force
:
true
})
);
});
it
(
'should return the sum of the values for a field named the same as an SQL reserved keyword'
,
function
()
{
...
...
@@ -2040,6 +2055,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it
(
'should accept a where clause with custom fields'
,
function
()
{
return
this
.
UserWithFields
.
bulkCreate
([
{
age
:
2
,
gender
:
'male'
},
{
age
:
3
,
gender
:
'female'
}
],
{
logging
:
console
.
log
}).
bind
(
this
).
then
(
function
()
{
return
expect
(
this
.
UserWithFields
.
sum
(
'age'
,
{
where
:
{
'gender'
:
'male'
}
})).
to
.
eventually
.
equal
(
2
);
});
});
it
(
'allows sql logging'
,
function
()
{
var
logged
=
false
;
return
this
.
UserWithAge
.
sum
(
'age'
,
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment