On 2 Apr 2014, at 22:44, Jonathan Vanasco <[email protected]> wrote:
>
> I'd like to avoid that, if at all possible. that was actually a solution to
> an earlier thing i had a few weeks ago!
>
> I'm fine with doing suqueryloads instead of joinedloads or hitting the
> database multiple times.
>
> with my current design... building another query where i need to specify
> loads will be too much of an undertaking. the only shortcut i could take
> would be constructing the query by pulling the session out of the object,
> then piping in the object's id as the filter... and that would just be too
> messy.
It might not be that bad (completely untested):
import sqlalchemy as sa
import sqlalchemy.orm as saorm
def get_query(obj):
session = saorm.object_session(obj)
cls = type(obj)
identity = sa.inspect(obj).identity
primary_key = sa.inspect(cls).primary_key
query = session.query(cls)
condition = []
for col, val in zip(primary_key, identity):
condition.append(col == val)
query = query.filter(sa.and_(*condition))
return query
u = session.query(User).get(1)
get_query(u).options(joinedload_all('foo.bar')).first()
Simon
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.