Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread kris
Thank you very much . aliased (TreeNode, row_expression) was exactly what I was looking for. Never noticed that before. On Tuesday, November 10, 2020 at 12:51:30 PM UTC-8 Mike Bayer wrote: > I would need to see the table defs for "docorder" to do this completely. > > The parents + kids

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread Mike Bayer
I would need to see the table defs for "docorder" to do this completely. The parents + kids version: # 1. make parents cte parents_cte = session.query(TreeNode).filter(TreeNode.name == "subnode4").cte("parents", recursive=True) p2 = session.query(TreeNode).filter(TreeNode.id ==

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread kris
A more complete version of the SQL to be returned as TreeNode WITH RECURSIVE docorder AS ( select id, rn from ...), parents AS (SELECT tree.id AS id, tree.parent_id AS parent_id, tree.name AS name FROM tree WHERE tree.name = 'subnode4' UNION SELECT tree.id AS tree_id, tree.parent_id

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread kris
I really appreciate the help.. I've been stuck a while on this. I am including the SQL string which may help, but to elaborate I would like to receive ORM objects that I can further operate on. >From above, the line print session.query(parents).all() prints [(9, 7,

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread Mike Bayer
do you know what SQL string you want? I can do this quickly if you can send me the exact string. On Tue, Nov 10, 2020, at 1:40 PM, kris wrote: > The example is from adjecency_list.py example. > > The last three lines construct a recursive CTE to walk to the parent from a > found

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread kris
The example is from adjecency_list.py example. The last three lines construct a recursive CTE to walk to the parent from a found node. I cannot figure out how to return TreeNodes vs tuples. Thanks from sqlalchemy import Column from

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread Mike Bayer
Please provide a working and minimal mapping of your classes in question as well as the SQL you wish to emit. On Tue, Nov 10, 2020, at 12:07 PM, kris wrote: > > A little more detail.. > > N = alias (T) > node1 = session.query (N).filter(initial_node_filter).cte ('parents', > recursive =

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread kris
A little more detail.. N = alias (T) node1 = session.query (N).filter(initial_node_filter).cte ('parents', recursive = True) parents = topnode.union (DBSession.query(N).filter (N.id == node1.c.parent_id) print session.query(parents).all() shows an array 'result' How to force it return

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread kris
Initially I tried to remain solely in ORM constructs, I only broke down and used the SQL expressions after hitting a wall. A little more specifically: when I try: session.query (part1).all () I receive an array of class 'sqlalchemy.util._collections.result' while I was expecting an

Re: [sqlalchemy] CTEs and ORM objects.

2020-11-10 Thread Mike Bayer
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

[sqlalchemy] CTEs and ORM objects.

2020-11-09 Thread kris
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')