Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread HP3
I ended up settling for this: @hybrid_property def ancestors(self): session = inspect(self).session cte = session.query(P2B) \ .filter(P2B.id == self.id) \ .cte(name='cte', recursive=True) cte = cte.union(

Re: [sqlalchemy] Conditionally ordered nested relationships

2018-06-08 Thread Mike Bayer
On Fri, Jun 8, 2018 at 7:13 PM, Daniel Cardin wrote: > Aha i didn't realize I had a mismatch of relationship names > > Below I included a full example that fails an assert where I'm having the > issue. > > The only reason I have "unordered_things" and "things" relationships is > because I wasn't

Re: [sqlalchemy] Conditionally ordered nested relationships

2018-06-08 Thread Daniel Cardin
Aha i didn't realize I had a mismatch of relationship names Below I included a full example that fails an assert where I'm having the issue. - The only reason I have "unordered_things" and "things" relationships is because I wasn't sure how to get it to only include the query's sort -

Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread Mike Bayer
On Fri, Jun 8, 2018 at 6:59 PM, HP3 wrote: > :( > > Basically, I was trying to prevent the SQL Select for each child-to-parent > loop-iteration when navigating the tree from leaf to root because I figured > I could use CTE recursive as @hybrid_property.expression. > > I was trying to take full

Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread HP3
:( Basically, I was trying to prevent the SQL Select for each child-to-parent loop-iteration when navigating the tree from leaf to root because I figured I could use CTE recursive as @hybrid_property.expression. I was trying to take full advantage of @hybrid_property.expression like the

Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread Mike Bayer
On Fri, Jun 8, 2018 at 5:14 PM, HP3 wrote: > Thank you so much Mike > > > I am trying to write @ancestors.expression to produce a SQL like this: > > WITH RECURSIVE scte(id, parent_id) AS > > (SELECT p2bases.id AS id, p2bases.parent_id AS parent_id > > FROM p2bases > > WHERE p2bases.id = %(id_1)s

Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread HP3
Thank you so much Mike I am trying to write @ancestors.expression to produce a SQL like this: WITH RECURSIVE scte(id, parent_id) AS (SELECT p2bases.id AS id, p2bases.parent_id AS parent_id FROM p2bases WHERE p2bases.id = %(id_1)s --- This is the param I need at runtime (cls.id) UNION

Re: [sqlalchemy] Conditionally ordered nested relationships

2018-06-08 Thread Mike Bayer
On Fri, Jun 8, 2018 at 2:08 PM, Daniel Cardin wrote: > So "ParentFeature.shows" is there for the case where I query > `pg.query(Parent)`, I want it to be ordered ascending. > > The only reason i have "ParentFeature.unordered_shows" is because if I try noting that, I don't know what

Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread Mike Bayer
the example is huge can you point out which two queries you'd like to compare? this: cte = dbsession.query(P2B.id, P2B.parent_id) \ .filter(P2B.id == child_id) \ .cte(name='cte', recursive=True) cte = cte.union( dbsession.query(P2B.id, P2B.parent_id).filter(

Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread HP3
A!!! Makes perfect sense! But there is something still amiss ... The last 2 assertions are triggering because the query is returning a single ancestor (self) instead of the whole recursive list. See the plain CTE and the @hybrid_property.expression CTE: T*he where clauses are still

Re: [sqlalchemy] Conditionally ordered nested relationships

2018-06-08 Thread Daniel Cardin
So "ParentFeature.shows" is there for the case where I query `pg.query(Parent)`, I want it to be ordered ascending. The only reason i have "ParentFeature.unordered_shows" is because if I try to apply an `order_by` to `Thing.name` in a query (like my query example), it emits "ORDER BY

Re: [sqlalchemy] Conditionally ordered nested relationships

2018-06-08 Thread Mike Bayer
On Fri, Jun 8, 2018 at 11:40 AM, Daniel Cardin wrote: > I'm attempting to a sort of complex relationship filter/sort operation. I > need to filter results by the id of a parent relationship, and then sort a > nested relationship by one of its attributes > > class Parent(Base): > __tablename__

Re: [sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread Mike Bayer
On Fri, Jun 8, 2018 at 12:16 PM, HP3 wrote: > Attached the whole file OK, removing all the "zope" stuff as well as all the other queries that aren't noted as failing, I am only looking at this query: ancestors = dbsession.query(P2B.ancestors).filter(P2B.id == child_id).all() #

[sqlalchemy] Re: CTE and @hybrid_property.expression

2018-06-08 Thread HP3
Attached the whole file On Thursday, June 7, 2018 at 7:01:21 PM UTC-5, HP3 wrote: > > Hello > > > Having difficulty with CTE and @hybrid_property.expression on a adjacency > list model. > > > class P2B(Base): > __tablename__ = 'p2bases' > id = Column(Integer, primary_key=True) >

[sqlalchemy] Conditionally ordered nested relationships

2018-06-08 Thread Daniel Cardin
I'm attempting to a sort of complex relationship filter/sort operation. I need to filter results by the id of a parent relationship, and then sort a nested relationship by one of its attributes class Parent(Base): __tablename__ = 'parent' id = Column(types.Integer, primary_key=True,