I think what's important here is to come up with the exact SQL string you'd 
like to create first, then if you can share that here we can show you how 
SQLAlchemy can render it from the expression language.    As far as working 
with the PG functions someone else here might know or you can try stackoverflow.


On Thu, Nov 5, 2020, at 7:42 AM, Wojtek K wrote:
> Hi All, I'm trying to find the solution but with no success
> 
> I have 2 tables related through assoc table
> 
> table a
> id   |name    |
> -----|--------|
> 1    |example |
> 
> assoc
> id   |a_id | b_id |
> -----|-----|------|
> 1    |1    |1     |
> 2    |1    |2     |
> 
> table b  
> id   |name     |
> -----|---------|
> 1    |example1 |
> 2    |example2 |  
> 
> 
> Of course I've mapped classes through orm.
> I want to select all records from a join b and then group by a object and 
> aggregate all related records from b to an array of b objects:
> 
> ( <model.TableA>, [<model.TableB>, <model.TableB>] )
> 
> So far I only found a solution of using some mix of postgresql dialect and sa 
> like:
> postgresql.array_agg(func.json_build_object("id", TableB.id, "name", 
> TableB.name))
> and I can produce
> 
> ( <model.TableA>, [{"id":1, "name": "example1"}, {"id":2, "name": 
> "example2"}] )  
> 
> but ideally would by the array of objects
> 

> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/2d72d2bb-b07c-4220-92fb-dc7ce1839631n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/2d72d2bb-b07c-4220-92fb-dc7ce1839631n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/da440dbe-e4cf-468a-951f-a1dec2075a62%40www.fastmail.com.

Reply via email to