Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1) From the <a href="http://www.sqlalchemy.org/docs/orm/session.html#frequently-asked-questions" rel="nofollow">Session documentation</a>:</p> <blockquote> <p>[The Session] is somewhat used as a cache, in that it implements the identity map pattern, and stores objects keyed to their primary key. However, it doesn’t do any kind of query caching. ... It’s only when you say query.get({some primary key}) that the Session doesn’t have to issue a query.</p> </blockquote> <p>2.1) You are correct, relationships are not modified when you refresh an object.</p> <p>2.2) Yes, the group will be in the identity map.</p> <p>2.3) I believe your best bet will be to attempt to reload the entire group.items in a single query. From my experience it is usually much quicker to issue one large request than several smaller ones. The only time it would make sense to only reload a specific group.item is there was exactly one of them that needed to be loaded. Though in that case you are doing one large query instead of one small one so you don't actually reduce the number of queries.</p> <p>I have not tried it, but I believe you should be able to use the <a href="http://www.sqlalchemy.org/docs/orm/mapper_config.html?highlight=identity#sqlalchemy.orm.util.identity_key" rel="nofollow">sqlalchemy.orm.util.identity_key</a> method to determine whether an object is in sqlalchemy's identiy map. I would be interested to find out what calling identiy_key(Group, 83) returns.</p> <p>Initial Question) If I understand correctly you have an object that you fetched from the database where some of its relationships were eagerloaded and you would like to fetch the rest of the relationships with a single query? I believe you may be able to use the <a href="http://www.sqlalchemy.org/docs/orm/session.html#sqlalchemy.orm.session.Session.refresh" rel="nofollow">Session.refresh()</a> method passing in the the names of the relationships that you want to load.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. COYes, that's correct - i have already fetched from database object and i want to fetch its relationships in a single query (or 1 query per relationship, it doesn't matter as long as sqlalchemy can assign fetched properties to the object i'm working with). I tried refreshing as you suggested, sqlalchemy allows me to do it only for columns in the database, not relationships (i can refresh groupID of an item objeect, but i can't refresh group relationship). InvalidRequestError: No column-based properties specified for refresh operation. Use session.expire() to reload collections and related items.
      singulars
    2. COI think i can try triggering lazy loading of required properties and relationships (this would mean that 1 query per object will be issued), but what if i want to go several levels deeper in relationship level? Let's take an Item as example, i can trigger lazy loading by accessing item.group <1st query>, then i want sqlalchemy to fetch all items from item.group.items list of Item objects <+1>, and metaGroup for each item <+# of items> (it would look like ("group", "group.items", "group.items.metaGroup") for eager loading) - is there way to do this using 1 query for "group" property of an item?
      singulars
    3. COYou could do group = Session.query(item.group).options(eagerload(...)) and completely reload the group that the item is a part of. I am curious whether at that point item.group will == group.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload