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 6a7b2178
authored
Jun 29, 2015
by
Tim Perry
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not automatically add primary key to attributes (or include[].attributes) if raw is set
1 parent
3d0af749
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
7 deletions
lib/model.js
test/integration/include/findAll.test.js
lib/model.js
View file @
6a7b217
...
...
@@ -437,7 +437,7 @@ var validateIncludedElements = function(options, tableNames) {
// validate all included elements
var
includes
=
options
.
include
;
for
(
var
index
=
0
;
index
<
includes
.
length
;
index
++
)
{
var
include
=
includes
[
index
]
=
validateIncludedElement
.
call
(
this
,
includes
[
index
],
tableNames
);
var
include
=
includes
[
index
]
=
validateIncludedElement
.
call
(
this
,
includes
[
index
],
tableNames
,
options
.
raw
);
include
.
parent
=
options
;
// associations that are required or have a required child and is not a ?:M association are candidates for the subquery
...
...
@@ -461,7 +461,7 @@ var validateIncludedElements = function(options, tableNames) {
};
Model
.
$validateIncludedElements
=
validateIncludedElements
;
validateIncludedElement
=
function
(
include
,
tableNames
)
{
validateIncludedElement
=
function
(
include
,
tableNames
,
raw
)
{
if
(
!
include
.
hasOwnProperty
(
'model'
)
&&
!
include
.
hasOwnProperty
(
'association'
))
{
throw
new
Error
(
'Include malformed. Expected attributes: model or association'
);
}
...
...
@@ -476,17 +476,17 @@ validateIncludedElement = function(include, tableNames) {
tableNames
[
include
.
model
.
getTableName
()]
=
true
;
if
(
include
.
attributes
)
{
if
(
include
.
attributes
&&
!
raw
)
{
include
.
originalAttributes
=
include
.
attributes
.
slice
(
0
);
if
(
include
.
attributes
.
length
)
{
include
.
model
.
primaryKeyAttributes
.
forEach
(
function
(
attr
)
{
include
.
model
.
primaryKeyAttributes
.
forEach
(
function
(
attr
)
{
if
(
include
.
attributes
.
indexOf
(
attr
)
===
-
1
)
{
include
.
attributes
.
unshift
(
attr
);
}
});
}
}
else
{
}
else
if
(
!
include
.
attributes
)
{
include
.
attributes
=
Object
.
keys
(
include
.
model
.
tableAttributes
);
}
...
...
@@ -536,7 +536,7 @@ validateIncludedElement = function(include, tableNames) {
// Validate child includes
if
(
include
.
hasOwnProperty
(
'include'
))
{
validateIncludedElements
.
call
(
include
.
model
,
include
,
tableNames
);
validateIncludedElements
.
call
(
include
.
model
,
include
,
tableNames
,
raw
);
}
return
include
;
...
...
@@ -1178,7 +1178,8 @@ Model.prototype.findAll = function(options) {
validateIncludedElements
.
call
(
this
,
options
,
tableNames
);
if
(
options
.
attributes
)
{
// If we're not raw, we have to make sure we include the primary key for deduplication
if
(
options
.
attributes
&&
!
options
.
raw
)
{
if
(
options
.
attributes
.
indexOf
(
this
.
primaryKeyAttribute
)
===
-
1
)
{
options
.
originalAttributes
=
options
.
attributes
;
options
.
attributes
=
[
this
.
primaryKeyAttribute
].
concat
(
options
.
attributes
);
...
...
test/integration/include/findAll.test.js
View file @
6a7b217
...
...
@@ -1920,5 +1920,48 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect
(
parseInt
(
post
.
get
(
'commentCount'
),
10
)).
to
.
equal
(
3
);
});
});
it
(
'should not add primary key when including and aggregating with raw: true'
,
function
()
{
var
Post
=
this
.
sequelize
.
define
(
'Post'
,
{
title
:
DataTypes
.
STRING
})
,
Comment
=
this
.
sequelize
.
define
(
'Comment'
,
{
content
:
DataTypes
.
TEXT
});
Post
.
Comments
=
Post
.
hasMany
(
Comment
,
{
as
:
'comments'
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
Post
.
create
({
title
:
Math
.
random
().
toString
(),
comments
:
[
{
content
:
Math
.
random
().
toString
()},
{
content
:
Math
.
random
().
toString
()},
{
content
:
Math
.
random
().
toString
()},
]
},
{
include
:
[
Post
.
Comments
]
});
}).
then
(
function
()
{
return
Post
.
findAll
({
attributes
:
[],
include
:
[
{
association
:
Post
.
Comments
,
attributes
:
[[
this
.
sequelize
.
fn
(
'COUNT'
,
this
.
sequelize
.
col
(
'comments.id'
)),
'commentCount'
]]
}
],
raw
:
true
});
}).
then
(
function
(
posts
)
{
expect
(
posts
.
length
).
to
.
equal
(
1
);
var
post
=
posts
[
0
];
console
.
log
(
"** "
+
JSON
.
stringify
(
post
));
expect
(
post
.
id
).
not
.
to
.
be
.
ok
;
expect
(
parseInt
(
post
[
"comments.commentCount"
],
10
)).
to
.
equal
(
3
);
});
});
});
});
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