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 bbf9139d
authored
Feb 13, 2020
by
Pedro Augusto de Paula Barbosa
Committed by
GitHub
Feb 13, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
other(find-all): throw on empty attributes (#11867)
1 parent
0fb4bb11
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
0 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/mssql/query-generator.js
test/integration/model/findAll.test.js
lib/dialects/abstract/query-generator.js
View file @
bbf9139
...
@@ -1365,6 +1365,7 @@ class QueryGenerator {
...
@@ -1365,6 +1365,7 @@ class QueryGenerator {
}
}
if (subQuery) {
if (subQuery) {
this._throwOnEmptyAttributes(attributes.main, { modelName: model && model.name, as: mainTable.as });
query = `
SELECT
$
{
attributes
.
main
.
join
(
', '
)}
FROM
(
$
{
subQueryItems
.
join
(
''
)})
AS
$
{
mainTable
.
as
}
$
{
mainJoinQueries
.
join
(
''
)}
$
{
mainQueryItems
.
join
(
''
)}
`;
query = `
SELECT
$
{
attributes
.
main
.
join
(
', '
)}
FROM
(
$
{
subQueryItems
.
join
(
''
)})
AS
$
{
mainTable
.
as
}
$
{
mainJoinQueries
.
join
(
''
)}
$
{
mainQueryItems
.
join
(
''
)}
`;
} else {
} else {
query = mainQueryItems.join('');
query = mainQueryItems.join('');
...
@@ -2053,7 +2054,17 @@ class QueryGenerator {
...
@@ -2053,7 +2054,17 @@ class QueryGenerator {
return { mainQueryOrder, subQueryOrder };
return { mainQueryOrder, subQueryOrder };
}
}
_throwOnEmptyAttributes(attributes, extraInfo = {}) {
if (attributes.length > 0) return;
const asPart = extraInfo.as && `
as
$
{
extraInfo
.
as
}
` || '';
const namePart = extraInfo.modelName && `
for
model
'${extraInfo.modelName}'
` || '';
const message = `
Attempted
a
SELECT
query
$
{
namePart
}
$
{
asPart
}
without
selecting
any
columns
`;
throw new sequelizeError.QueryError(message.replace(/ +/g, ' '));
}
selectFromTableFragment(options, model, attributes, tables, mainTableAs) {
selectFromTableFragment(options, model, attributes, tables, mainTableAs) {
this._throwOnEmptyAttributes(attributes, { modelName: model && model.name, as: mainTableAs });
let fragment = `
SELECT
$
{
attributes
.
join
(
', '
)}
FROM
$
{
tables
}
`;
let fragment = `
SELECT
$
{
attributes
.
join
(
', '
)}
FROM
$
{
tables
}
`;
if (mainTableAs) {
if (mainTableAs) {
...
...
lib/dialects/mssql/query-generator.js
View file @
bbf9139
...
@@ -837,6 +837,8 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
...
@@ -837,6 +837,8 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
}
}
selectFromTableFragment
(
options
,
model
,
attributes
,
tables
,
mainTableAs
,
where
)
{
selectFromTableFragment
(
options
,
model
,
attributes
,
tables
,
mainTableAs
,
where
)
{
this
.
_throwOnEmptyAttributes
(
attributes
,
{
modelName
:
model
&&
model
.
name
,
as
:
mainTableAs
});
const
dbVersion
=
this
.
sequelize
.
options
.
databaseVersion
;
const
dbVersion
=
this
.
sequelize
.
options
.
databaseVersion
;
const
isSQLServer2008
=
semver
.
valid
(
dbVersion
)
&&
semver
.
lt
(
dbVersion
,
'11.0.0'
);
const
isSQLServer2008
=
semver
.
valid
(
dbVersion
)
&&
semver
.
lt
(
dbVersion
,
'11.0.0'
);
...
...
test/integration/model/findAll.test.js
View file @
bbf9139
...
@@ -60,6 +60,38 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -60,6 +60,38 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
it
(
'should throw on an attempt to fetch no attributes'
,
function
()
{
return
expect
(
this
.
User
.
findAll
({
attributes
:
[]
})).
to
.
be
.
rejectedWith
(
Sequelize
.
QueryError
,
/^Attempted a SELECT query.+without selecting any columns$/
);
});
it
(
'should not throw if overall attributes are nonempty'
,
function
()
{
const
Post
=
this
.
sequelize
.
define
(
'Post'
,
{
foo
:
DataTypes
.
STRING
});
const
Comment
=
this
.
sequelize
.
define
(
'Comment'
,
{
bar
:
DataTypes
.
STRING
});
Post
.
hasMany
(
Comment
,
{
as
:
'comments'
});
return
Post
.
sync
({
force
:
true
})
.
then
(()
=>
Comment
.
sync
({
force
:
true
}))
.
then
(()
=>
{
// Should not throw in this case, even
// though `attributes: []` is set for the main model
return
Post
.
findAll
({
raw
:
true
,
attributes
:
[],
include
:
[
{
model
:
Comment
,
as
:
'comments'
,
attributes
:
[
[
Sequelize
.
fn
(
'COUNT'
,
Sequelize
.
col
(
'comments.id'
)),
'commentCount'
]
]
}
]
});
});
});
describe
(
'special where conditions/smartWhere object'
,
()
=>
{
describe
(
'special where conditions/smartWhere object'
,
()
=>
{
beforeEach
(
function
()
{
beforeEach
(
function
()
{
this
.
buf
=
Buffer
.
alloc
(
16
);
this
.
buf
=
Buffer
.
alloc
(
16
);
...
...
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