Re: [sqlalchemy] Optimally access a model paramater n number of relationships away

2016-04-10 Thread Mike Bayer



On 04/06/2016 06:55 PM, Brian Leach wrote:

Hi everyone!

I have asked this question on StackOverflow, please see it for full
detail of how the models are laid
out: 
http://stackoverflow.com/questions/36463623/optimally-access-an-sqlalchemy-model-paramater-n-number-of-relationships-away

I have some "test" models that all have a relationship back to a
"location" model, each a varying number of relationships away. When I
want to get the name of the "location" associated with a "test", there
is a ton of SQL emitted. Is it possible to build in some sort of
shortcut to the location relationship?


yes, however you have to decide what kind of SQL you'd want to emit for 
this.   The examples at 
http://docs.sqlalchemy.org/en/rel_1_0/orm/join_conditions.html#composite-secondary-joins 
and 
http://docs.sqlalchemy.org/en/rel_1_0/orm/join_conditions.html#relationship-to-non-primary-mapper 
illustrate some advanced techniques for building relationships that 
directly load across many tables, without loading the intermediary objects.


Otherwise, for garden-variety reduction of SQL, the joinedload() 
directive as was pointed out in the SO answer is typically the more 
expedient approach to loading a series of object relationships, but in 
this case you're also loading all the intermediate collections into memory.





I have tried setting the 'lazy' parameter on the relationship to
'select', 'joined', and 'subquery' although these do not seem to make a
significant difference when it comes to tying a test model to its location.

The way I have things defined also make it difficult for me to filter a
group of tests based on their location, since the relationship is so
different with each. I do not even know how to join these tables using
raw SQL or how to begin to approach trying to emulate what the ORM is
constructing for me.

I would imagine that this has been nailed before and am hoping that one
of you out there can fill me in on the missing concept.


  - Brian

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


--
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] Optimally access a model paramater n number of relationships away

2016-04-06 Thread Brian Leach
Hi everyone!

I have asked this question on StackOverflow, please see it for full detail 
of how the models are laid 
out: 
http://stackoverflow.com/questions/36463623/optimally-access-an-sqlalchemy-model-paramater-n-number-of-relationships-away

I have some "test" models that all have a relationship back to a "location" 
model, each a varying number of relationships away. When I want to get the 
name of the "location" associated with a "test", there is a ton of SQL 
emitted. Is it possible to build in some sort of shortcut to the location 
relationship?

I have tried setting the 'lazy' parameter on the relationship to 'select', 
'joined', and 'subquery' although these do not seem to make a significant 
difference when it comes to tying a test model to its location.

The way I have things defined also make it difficult for me to filter a 
group of tests based on their location, since the relationship is so 
different with each. I do not even know how to join these tables using raw 
SQL or how to begin to approach trying to emulate what the ORM is 
constructing for me.

I would imagine that this has been nailed before and am hoping that one of 
you out there can fill me in on the missing concept.


 - Brian

-- 
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.