Jonathan Vanasco <jvana...@gmail.com> writes:
> On Thursday, January 5, 2017 at 8:34:52 PM UTC-5, Daniel Kraus wrote:
>>
>> The use-case is that I have a big model with lots of complex
>> relationships but 90% of the time I don't need the data from those.
>
> If I'm reading your question correctly, most of what sqlalchemy does (and
> excels at) is specifically keeping people from doing what you're trying to
> do.

I think you got me wrong then.

> It seems like you're trying to avoid all the work that is done to
> ensure data integrity across sessions and transactions.  (Which is a common
> need)

Nope,
I just want to use a cache where I only store only the DB row for my
model and not all data from relation tables as well.

> Read up on the `merge` session
> method 
> (http://docs.sqlalchemy.org/en/latest/orm/session_state_management.html#merging)
>  The dogpile caching section is largely based on that (though goes beyond
> it).
>
> You would do something like this:
>
>     user = User(**userdata)
>     user = session.merge(user)
>
> That will merge the user object into the session (and return the merged
> object).

Thanks.


> You will run into problems if your cached data is incomplete though --
> AFAIK, there is no way to tell sqlalchemy that you've only loaded data for
> certain columns.  If you don't populate all the columns in your cache, but
> have it in the db, I have no idea how to get that info from the db.

`session.merge` populates the missing attributes from the db.
But in my case, where I have all the data and know it's up to date,
I can even skip that one select and use the `load=False` parameter.
So for my example I can (thanks to Mike's reply):

  make_transient_to_detached(user)
  user = session.merge(user, load=False)

and after that I can do my `user.big_n_to_m_relation_data` and it will
correctly query the db.


Thanks,
  Daniel

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