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

2018-06-13 Thread HP3
Hmmm - makes sense. I'll switch to plain @property instead. Thank you very much again Mike! On Tuesday, June 12, 2018 at 3:32:35 PM UTC-5, Mike Bayer wrote: > > On Tue, Jun 12, 2018 at 12:20 PM, HP3 > > wrote: > > Hmmm > > > > In my testing, the class level functionality doesn't seem to be

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

2018-06-12 Thread Mike Bayer
On Tue, Jun 12, 2018 at 12:20 PM, HP3 wrote: > Hmmm > > In my testing, the class level functionality doesn't seem to be used ever. > Only the instance level. yup, because you can't do inspect(cls).session, that wouldn't have worked. hence safer to use @property so that your code does not imply

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

2018-06-12 Thread HP3
Hmmm In my testing, the class level functionality doesn't seem to be used ever. Only the instance level. I thought the class level functionality would be used if the @hybrid_property.expression was declared: In this case, it was the recursive CTE that didn't get the `cls.id` "automagically"

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

2018-06-12 Thread Mike Bayer
On Tue, Jun 12, 2018, 10:20 AM HP3 wrote: > Hmmm > > I didn't use the @hybrid_property.expression for anything else so I ended > up not using it. > > However, I still use @hybrid_property ... wouldn't it be the same as > @property? > > (I thought they were "pretty much" the same) > They're

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

2018-06-12 Thread HP3
Hmmm I didn't use the @hybrid_property.expression for anything else so I ended up not using it. However, I still use @hybrid_property ... wouldn't it be the same as @property? (I thought they were "pretty much" the same) On Saturday, June 9, 2018 at 8:06:18 AM UTC-5, Mike Bayer wrote: > >

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

2018-06-09 Thread Mike Bayer
That's fine but you should probably use a normal @property for that, unless you are still making use of a separate "expression" portion. On Fri, Jun 8, 2018, 8:04 PM HP3 wrote: > I ended up settling for this: > > @hybrid_property > > def ancestors(self): > > session =

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] 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] 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] 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) >