I think it's starting to make sense.  I did misunderstand exactly when the 
.expression SQL was used - I had thought even queries for ORM models would 
use it, but I see it does work as you describe.

My goal is to return a subset of fields and make that query building 
operation be dynamic and "simple" for downstream services.  My trouble 
seems to be that as I move away from the baseline full "query(MyORMClass)", 
the relationship configured joins no longer automatically come along, so I 
have to re-specify them in the query.

Thanks for the response, I will consider the column_property alternative - 
it might be better for our situation.

As far as limiting fields returned then though - I think these are my 
options, if I understand them right:

query(ORM_Class)
-Returns list of ORM Model instances
-The only option that auto-includes all joins defined in ORM Model 
relationships.
-Does not calculate hybrid_property in SQL
-Referencing hybrid_property on the returned ORM Model evaluates 
hybrid_property in python.

load_only(ORM_Class.column_attr,... etc)
-Returns list of ORM Models (sparse, lazily populated)
-Will *NOT* add necessary joins from relationship definitions in Model.  
Must join manually in query.
-Can't ask for hybrid_property in query
-Referencing hybrid_property on the returned ORM Model still evaluates 
hybrid_property in python.

query(ORM_Class.column_attr, ORM_Class.hybrid_property)
-Returns Tuple
-Will *NOT* add necessary joins from relationship definitions in Model.  
Must join manually in query.
-Uses the .expression form of the hybrid_property to SELECT/calculate it in 
SQL

with_entities(ORM_Class.column_attr,... etc)
-Returns Tuple
-Will *NOT* add necessary joins from relationship definitions in Model.  
Must join manually in query.
-Uses the .expression form of the hybrid_property to SELECT/calculate it in 
SQL




On Monday, June 3, 2019 at 2:21:41 PM UTC-4, Mike Bayer wrote:
>
> well hybrids don't apply to a load_only operation because they are not 
> included in the query that emits when the ORM SELECTs for an object.    
> that is, @hybrid_property.expression gives you a SQL expression, but that's 
> never included when you say session.query(Task).   It's only if you said, 
> session.query(Task).filter(Task.hybrid_test == 'x').
>
> There's a construct called column_property() that *is* included in the 
> columns when you say query(Task).  This is actually the kind of property 
> that's used for all the regular columns but it also accomodates arbitrary 
> SQL expressions.   to that extent there's some overlap between 
> column_property and hybrids which is why they are both listed at 
> https://docs.sqlalchemy.org/en/13/orm/mapped_sql_expr.html. 
> <https://docs.sqlalchemy.org/en/13/orm/mapped_sql_expr.html>
>
> let me know if that makes sense.
>
> On Mon, Jun 3, 2019, at 2:17 PM, Gmoney wrote:
>
> I should at least clarify that I have @hybrid_test.expression in place for 
> that property and it works when I just do the base 'get me everything' 
> query.
>
>
> On Monday, June 3, 2019 at 2:13:17 PM UTC-4, Gmoney wrote:
>
> I'm trying to use load_only and was able to get a really basic example to 
> work.  My problem is that once I try to load one of my hybrid properties, 
> it fails.  
>
> This code works for any number of basic Column properties but fails once I 
> add the hybrid property:
>
> columns = (Task.msg_id, Task.hybrid_test)
>
> q = (db.session.query(Task).options(load_only(*columns).lazyload('*')))
>
> AttributeError: Neither 'hybrid_property' object nor 'ExprComparator' 
> object associated with Task.hybrid_test has an attribute 'property'
>
>
> Just wondering if I should be able to get this to work if I keep digging 
> or post a more thorough code snippet, or is it not possible using this 
> approach?
>
>
> --
> 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 sqlal...@googlegroups.com <javascript:>.
> To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/2661a10c-4741-4e5b-a1e7-2db578c5712b%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/2661a10c-4741-4e5b-a1e7-2db578c5712b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/1c1229fe-80a1-42b6-8cfd-8337ea6933b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to