Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FIX Empty fields on SQLQuery->aggregate() with alias

Breaks Postgres otherwise, because it produces a
"SELECT *, <field> ... " statement without putting all
fields into the GROUP BY.
  • Loading branch information...
commit 6d5925708eb4f4a296ebc56077a1fa6dc08cc802 1 parent 2bc273e
@chillu chillu authored
Showing with 1 addition and 0 deletions.
  1. +1 −0  model/SQLQuery.php
View
1  model/SQLQuery.php
@@ -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);

1 comment on commit 6d59257

@halkyon
Owner

@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.
Something went wrong with that request. Please try again.