On Tue, Feb 5, 2019 at 9:22 AM Gmoney <grgg...@gmail.com> wrote:
>
> We have some elaborate logic in a a @hybrid_property and 
> @<hybrid_property>.expression that I'm keen on setting up some testing to 
> ensure that both cases return the same value.  I'm having some difficulty 
> sorting out when each version is used at any given time though.
>
>         @hybrid_property
>         def testProp(self):
>              ...etc...
>
>         @testProp.expression(cls):
>              ...etc...
>
>         ...
>
>         # This query generated from this does not include 'test_prop' in the 
> select.
>         orm_obj = Task.query.filter(Task.id == '30340').one()
>         # It then executes through the @hybrid_property (python) code when 
> you access it.
>         print (orm_obj.test_prop)
>
>         # This version runs through the @testProp.expression at query 
> runtime, generating the value from the select
>         tuples = Task.query.filter(Task.id == 
> '30340').with_entities(Task.test_prop)
>         # And of course accessing the tuple here doesn't run the 
> @hybrid_property code, as it's already there.
>         print (tuples[0].test_prop)
>
>
> I understand that one is an instance vs a class property, but during the 
> query processing it's not clear to me when/why one would be used vs the 
> other.  Is the 'with_entities' doing something here?

when you run Task.test_prop, you just ran your hybrid's @expression
function.   with_entities didn't run it, you did.

> What other cases would the expression version be used?

any time you use Task.test_prop, e.g., call it from the class.     It
will never be run otherwise.   all occurrences of your @expression
function running are in your own source code, not in SQLAlchemy, which
knows nothing about this attribute.


> I'm just looking for some help understanding the when/where/why of these. I'd 
> really love to NOT write the expression version at all if possible (we won't 
> need to filter on it in the query or anything),

then you should change it to raise NotImplementedError().  you will
then know immediately where it is being called :) and those places can
be fixed (since they are all on your end).

> because in our case, it's just begging for us to introduce bugs via 
> inconsistent implementations of the same intended logic.

so you should not use @hybrid_property.  Use a regular Python @property.


>
> --
> 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 post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to