Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6367031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:29:51+00:00 2026-05-25T00:29:51+00:00

If I have an sqlalchemy-mapped instance. Can I get an underlying dynamic query object

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:29:51+00:00Added an answer on May 25, 2026 at 12:29 am

    I think you’re describing the lazy="dynamic" property of relationship(). something like

     class Employee(Base):
         __table_name__ = "employees"
         ...
    
         projects = relationship(..., lazy="dynamic")
    

    which will cause Employee().project to return a sqlalchemy.orm.Query instance 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 two relationship()s instead.

     class Employee(Base):
         __table_name__ = "employees"
         ...
    
         projects_query = relationship(..., lazy="dynamic")
         projects = relationship(..., lazy="select")
    

    edit: You said

    I need somehow to get the dynamic query object of an already lazy relationship mapped property.

    Supposing we have an instance i of class Foo related to a class Bar by the property bars. First, we need to get the property that handles the relationship.

    from sqlalchemy.orm.attributes import manager_of_class
    p = manager_of_class(Foo).mapper.get_property('bars')
    

    We’d like an expression that and_s together all of the columns on i that relate it to bars. If you need to operate on Foo through an alias, substitute it in here.

    e = sqlalchemy.and_(*[getattr(Foo, c.key) == getattr(i, c.key)
                          for c in p.local_side])
    

    Now we can create a query that expresses this relationship. Substitute aliases for Foo and Bar here as needed.

    q = session.query(Foo) \
               .filter(e) \
               .join(Foo.bars) \
               .with_entities(Bar)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question regarding the SQLAlchemy. How can I add into my mapped class
I've been trying to map an object to a database using SQLAlchemy but have
Is it possible to have multi-level polymorphism in SQLAlchemy? Here's an example: class Entity(Base):
I have three tables (users, articles, and tags) defined in SQLAlchemy and mapped with
I'm using Turbogears2 and SQLAlchemy to develop a webapp. I have two mapped tables
I access a a postgres table using SQLAlchemy. I want a query to have
I'm using Flask framework with sqlalchemy and I can't query some related table of
I have a simple One-to-Many relation mapped with SqlAlchemy: Base = declarative_base() class Type(Base):
I have a number of classes that are mapped to tables with SQLAlchemy (non-declaratively
Can someone explain, how can I avoid application freezing when I, for example, have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.