If I have an sqlalchemy-mapped instance. Can I get an underlying dynamic query object corresponding to an attribute of said instance?
For example:
e = Employee()
e.projects
#how do I get a query object loaded with the underlying sql of e.projects
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think you’re describing the
lazy="dynamic"property ofrelationship(). something likewhich will cause
Employee().projectto return asqlalchemy.orm.Queryinstance instead of a collection containing the related items. However, that means there’s no (simple) way to access the collection directly. If you still need that (most likely you really do want it to be lazily loaded, set up tworelationship()s instead.edit: You said
Supposing we have an instance
iof classFoorelated to a classBarby the propertybars. First, we need to get the property that handles the relationship.We’d like an expression that
and_s together all of the columns onithat relate it tobars. If you need to operate onFoothrough an alias, substitute it in here.Now we can create a query that expresses this relationship. Substitute aliases for
FooandBarhere as needed.