[PonyORM-list] Distinct result?

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Fri Jul 1 10:29:58 UTC 2016


Hi Matthew!

I the future we can add new function `group_to_list`:

    select((p, group_to_list(c)) for p in Person for c in p.cars if c.make
in ("Toyota", "Honda"))

This function will perform aggregation in SQL. In PostgreSQL it will be
translated to `array_agg`, in Oracle to `collect`, in MySQL and SQLite to
`group_concat`.

Right now in your case it is necessary to perform grouping in Python:

    import itertools
    query = select((p, c) for p in Person for c in p.cars if c.make in
("Toyota", "Honda"))
    result = [
        (person, [ car for person, car in group ])
        for person, group in itertools.groupby(query, key=lambda row:
row[0])  # row[0] is person
    ]


P.S.: By the way, did you try to use db_session(strict=True)? I'm curious
to know if it reduced the memory usage in your application

Regards,
Alexander


On Fri, Jul 1, 2016 at 12:57 AM, Matthew Bell <matthewrobertbell at gmail.com>
wrote:

> Hello, if I have this query (from the docs)
>
>
> select((p, c) for p in Person for c in p.cars if c.make in ("Toyota", "Honda"))
>
>
> How can I have p be distinct, while c is not distinct?
>
> select((distinct(p), c) for p in Person for c in p.cars if c.make in ("Toyota", "Honda"))
>
> doesn't seem to work.
>
> Thanks!
>
>
>
> --
> Regards,
>
> Matthew Bell
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20160701/60456c35/attachment.html>


More information about the ponyorm-list mailing list