I would advise against using from_statement().   the Query object supports 
cte() and subqueries directly, see the example at 
https://docs.sqlalchemy.org/en/13/orm/query.html?highlight=query%20cte#sqlalchemy.orm.query.Query.cte

alternatively if you are using 1.4 beta1 then you can use session.execute() 
directly using a select() object.



On Tue, Nov 10, 2020, at 2:37 AM, kris wrote:
> 
> I need to query over a  table mapped to class T, 
> I usually use session.query (T), however I am using two cte's                 
>                                        
> part0 =  ... cte('part0')
> part1 = ... cte('part1')                                                      
>                                                                   part2 = ... 
> cte('aprt2')                                                                  
>                                                           allparts = union 
> (part1.select(), part2.select())                                              
>                                     
> session.query (allparts)  .. returns row record but I need it to return 
> objects of type T?     
> 
> I have tried using   q1 = session.query(T).from_statement (allnodes) 
> which appears to convert and create objects of T, However, 
> I cannot filter or order_by
> 
> I have tried using the sql epxression level:
> n = allparts.alias()
>  DBSession.query (T).from_statement (select ([n]).where (...).order_by 
> (...)).all()
> or 
>  DBSession.query(T).from_statement(DBSession.query 
> (n).filter(...).order_by(...).statement).
> 
> but I get NoSuchColumnError: "Could not locate column in row for column 
> 'T.id'"
> 
> Is there a way to let SA know how map the columns of the select expression to 
> an object?
> 
> Thanks.
> 

> -- 
> 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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/7f27eaff-1f0e-40a9-a225-464414d04041n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/7f27eaff-1f0e-40a9-a225-464414d04041n%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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/ce70fbd2-657a-43e3-b782-15b70962fbfc%40www.fastmail.com.

Reply via email to