https://github.com/web2py/pydal/pull/new/with_alias
Unfortunately it breaks pydal tests and I am not going to merge until I give it some more thought.
Let me explain the problem.
The rows in db(...).select(...) do not have a fixed structure as they depend on what is being selected.
- The result of a select involving one table if a list of dict of columns of the table { "field1": ..., "field2":...}
- The result of a select involving more than one one table is a list of dict of dicts {"table1: { "field1": ..., "field2":...}, "table2": ...}
- Expressions columns go in _extra {"table1: { "field1": ..., "field2":...}, "_extra": ...} which also make the output a dict of dicts
(this is because the expression is technically not a table column)
- Aliased expressions also go in _extra for the same reason above. It is not obvious to pydal that an aliased expression belongs to a single table and in general it does not.
**Aliasing was an afterthought introduced to handle multiple joins of the same table, not to rename fields.**
One can alias an expression that is just a field (although that is pointless). In this case things get confusing because one expects the aliased field to go in the table, but it goes in _extra and changes the dict structure. By adding any expression to the select (even if it is a single aliased field from one table) converts the rows from a dict to a dict of dicts.
Internally a rows is always a dict of dicts but it collapses to a single dict when (at top level) there is a single key (a single column and no expressions). In the branch above I change this behavior so that is collapses to a single dict when there is a single column even if there are expressions, by joining the table dict with the _extra dict.
This makes the case in laundmo's example behave like he expects, but it breaks existing tests and therefore it is not backward compatible. Also I am not convinced this is more intuitive.
Need to sleep on this...
Massimo