Skip to content

Commit

Permalink
FIX Empty fields on SQLQuery->aggregate() with alias
Browse files Browse the repository at this point in the history
Breaks Postgres otherwise, because it produces a
"SELECT *, <field> ... " statement without putting all
fields into the GROUP BY.
  • Loading branch information
chillu committed Apr 3, 2013
1 parent 2bc273e commit 6d59257
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions model/SQLQuery.php
Expand Up @@ -1071,6 +1071,7 @@ public function aggregate($column, $alias = null) {
$clone->setOrderBy($this->orderby);
$clone->setGroupBy($this->groupby);
if($alias) {
$clone->setSelect(array());
$clone->selectField($column, $alias);
} else {
$clone->setSelect($column);
Expand Down

1 comment on commit 6d59257

@halkyon
Copy link
Contributor

@halkyon halkyon commented on 6d59257 Apr 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chillu Did you ever get something like this? ...see below:

mssql_query(): message: Column "SiteTree_Live.Sort" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

This most likely happens with PostgreSQL as well. You can check this by simply using a <% cached 'something', List(Page).max(LastEdited) %> block in the template. It will try querying for the aggregate and fail.

e53280c seems to have introduced the ability to sort an aggregate. DataQuery::initialiseQuery() will add "Sort" as a default sorting on all queries to Page (SiteTree::$default_sort), but it won't add a GROUP BY automatically, thus causing the error. MSSQL complains if you sort an aggregate without specifying a GROUP BY clause. Seems like these template aggregates don't need any sorting or grouping anyway, so we could probably remove those.

Thoughts?

EDIT: Proposed fix in this pull request: #1712

Please sign in to comment.