Sqlalchemy Func.group_concat And Random Order Of Data
I have a following query which is using func.group_concat to group some data r = aliased(Rank) t = aliased(Team) p = aliased(Participant) players_ranks_data = ( db.session.q
Solution 1:
SQL results are (multi) sets and as such have no ordering, unless you explicitly define one. This applies to group rows produced by GROUP BY
as well; there is no order within the groups, and so aggregates receive values in any which order the current query depending on plan, physical layout, scheduling etc. happens to produce. The solution is to explicitly define the order:
func.group_concat(t.name.op("ORDER BY")(t.name))
This uses a generic operator function to produce the required SQL syntax. SQLAlchemy provides the aggregate_order_by
helper for the very same syntax, but it is provided for the Postgresql dialect only.
Post a Comment for "Sqlalchemy Func.group_concat And Random Order Of Data"