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

Commit bbf9139d by Pedro Augusto de Paula Barbosa Committed by GitHub

other(find-all): throw on empty attributes (#11867)

1 parent 0fb4bb11
......@@ -1365,6 +1365,7 @@ class QueryGenerator {
}
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('')}`;
} else {
query = mainQueryItems.join('');
......@@ -2053,7 +2054,17 @@ class QueryGenerator {
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) {
this._throwOnEmptyAttributes(attributes, { modelName: model && model.name, as: mainTableAs });
let fragment = `SELECT ${attributes.join(', ')} FROM ${tables}`;
if (mainTableAs) {
......
......@@ -837,6 +837,8 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
}
selectFromTableFragment(options, model, attributes, tables, mainTableAs, where) {
this._throwOnEmptyAttributes(attributes, { modelName: model && model.name, as: mainTableAs });
const dbVersion = this.sequelize.options.databaseVersion;
const isSQLServer2008 = semver.valid(dbVersion) && semver.lt(dbVersion, '11.0.0');
......
......@@ -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', () => {
beforeEach(function() {
this.buf = Buffer.alloc(16);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!