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

  • SEARCH
  • Home
  • 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 9186979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:39:42+00:00 2026-06-17T19:39:42+00:00

I need to query multiple entities, something like session.query(Entity1, Entity2) , only from a

  • 0

I need to query multiple entities, something like session.query(Entity1, Entity2), only from a subquery rather than directly from the tables. The docs have something about selecting one entity from a subquery but I can’t find how to select more than one, either in the docs or by experimentation.

My use case is that I need to filter the tables underlying the mapped classes by a window function, which in PostgreSQL can only be done in a subquery or CTE.

EDIT: The subquery spans a JOIN of both tables so I can’t just do aliased(Entity1, subquery).

  • 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-06-17T19:39:43+00:00Added an answer on June 17, 2026 at 7:39 pm
    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class A(Base):
        __tablename__ = "a"
    
        id = Column(Integer, primary_key=True)
        bs = relationship("B")
    
    class B(Base):
        __tablename__ = "b"
    
        id = Column(Integer, primary_key=True)
    
        a_id = Column(Integer, ForeignKey('a.id'))
    
    e = create_engine("sqlite://", echo=True)
    Base.metadata.create_all(e)
    s = Session(e)
    s.add_all([A(bs=[B(), B()]), A(bs=[B()])])
    s.commit()
    
    # with_labels() here is to disambiguate A.id and B.id.
    # without it, you'd see a warning
    # "Column 'id' on table being replaced by another column with the same key."
    subq = s.query(A, B).join(A.bs).with_labels().subquery()
    
    
    # method 1 - select_from()
    print s.query(A, B).select_from(subq).all()
    
    # method 2 - alias them both.  "subq" renders
    # once because FROM objects render based on object
    # identity.
    a_alias = aliased(A, subq)
    b_alias = aliased(B, subq)
    print s.query(a_alias, b_alias).all()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have function which is something like this function multiple_delete($entity1,$entity2,$entity2) { $query = SELECT
I need to execute a query To retrieve data from multiple tables but I'm
I need some help with query from multiple tables. My database: I have following
I need to query Azure Table Storage multiple times from different tables then combine
Need to query my SQL server from Access using an ADO connection (for example),
I need to query large amount of data from a database via C# and
i need to perform a insert query for multiple rows whereby the first column
I need to group my query where multiple columns match. For example, group all
Is it possible to filter query by multiple types in freebase API? I need
I'm creating a complex query with multiple tables and need to list the result.

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.