Hi @all. In regards of this thread
https://groups.google.com/d/msg/web2py/05waBr6aBxU/y91mwAEy-wYJthe code needs rethinking as a whole.
1st issue: any query that counts the records (except SQLite, that doesn't make any sense out of such a query) is mediumly borked. T-SQL needs an aliased subquery to work things out in most dbs....
this
select count(*) from (subselect)
needs to be
select count(*) from (subselect) aliased_subselect
so, at least, the code counting the rows doesn't go into exception (sqlhtml.py, line 1838)
'select count(*) from (%s) aliased_resultset;' %
works
so, 1st issue patched and ready to be used.
2nd issue: grid's default behaviour is to include in a select the id of the table (lines 1932-1935). Now, and that's the "majorly borked" part... . If you have a query with a groupby, any field requested HAS to be included in the groupby itself or be an aggregate. We can't (of course) include the PK in the groupby field, because it will simply not group anything.
Don't let SQLite fool you: when it does a groupby, it just "slims down" the resultset assigning to the unaggregate columns the first value returned by the driver. So, ok, it doesn't throw an exception, but the resultset is not meaningful.
Here's the big deal: what you edit when you press the edit button on a groupby(ed) grid ?
In SQLite, a random record picked out of the group.
In postgresql the whole grid fails badly (as it should), as long as you don't specify any field_id field .
The only way to have it working is in fact using one of the groupby(ed) fields as a field_id, but that doesn't make sense either, given the lack of support for editing a bunch of rows or to edit anything that is not a PK. So, it works but you can't view, you can't edit, you can't delete.
tl;dr: should we disable editing, viewing, deleting when groupby is used ? Do we even want to support such a flaky behaviour of the grid (meaning that to make it work you need to do groupby=field, field_id=field) ?