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 1042957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:35:23+00:00 2026-05-16T15:35:23+00:00

Is there a way in sqlalchemy to turn off declarative’s polymorphic join loading, in

  • 0

Is there a way in sqlalchemy to turn off declarative’s polymorphic join loading, in a single query? Most of the time it’s nice, but I have:

class A(Base) : 
   discriminator = Column('type', mysql.INTEGER(1), index=True, nullable=False)
   __mapper_args__ = { 'polymorphic_on' : discriminator }
   id = Column(Integer, primary_key=True)
   p = Column(Integer)

class B(A) : 
   __mapper_args__ = { 'polymorphic_identity' : 0 }
   id = Column(Integer, primary_key=True)
   x = Column(Integer)

class C(A) : 
   __mapper_args__ = { 'polymorphic_identity' : 1 }
   id = Column(Integer, primary_key=True)
   y = Column(String)

I want to make a query such that I get all A.ids for which B.x > 10, if that A is actually a B, or where C.y == ‘blah’, if that A is actually a C, all ordered by p.

To do it iteratively, I’m starting just with the first part – “get all A.id for which B.x > 10 if that A is actually a B”. So I thought I would start with an outer join:

session.query(A.id).outerjoin((B, B.id == A.id)).filter(B.x > 10)

… except there seems to be no way to avoid having that outerjoin((B, B.id == A.id)) clause generate a full join of everything in A to everything in B within a subselect. If B doesn’t inherit from A, then that doesn’t happen, so I’m thinking it’s the polymorphic declarative code generation that does that. Is there a way to turn that off? Or to force the outerjoin to do what I want?

What I want is something like this:

select a.id from A a left outer join B b on b.id == a.id where b.x > 10

but instead I get something like:

select a.id from A a left outer join (select B.id, B.x, A.id from B inner join A on B.id == A.id)

… as an aside, if it’s not possible, then is the latter less efficient than the former? Will the sql engine actually perform that inner join, or will it elide it?

  • 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-16T15:35:24+00:00Added an answer on May 16, 2026 at 3:35 pm

    You could try building the queries for each subclass individually, then unioning them together. When querying B.id, SQLAlchemy implicitly joins the superclass and returns A.id instead, so taking the union of selects for B.id and C.id only returns a single column.

    >>> b_query = session.query(B.id).filter(B.x > 10)
    >>> c_query = session.query(C.id).filter(C.y == 'foo')
    >>> print b_query.union(c_query)
    SELECT anon_1."A_id" AS "anon_1_A_id" 
    FROM (SELECT "A".id AS "A_id" 
    FROM "A" JOIN "B" ON "A".id = "B".id 
    WHERE "B".x > ? UNION SELECT "A".id AS "A_id" 
    FROM "A" JOIN "C" ON "A".id = "C".id 
    WHERE "C".y = ?) AS anon_1
    

    You still get a subselect, but only a single “layer” of joins – the outer select is just renaming the column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to retain your NSArray somewhere before passing it… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer PHP page which will then parse the data and CURL… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer You should be using LocalDate for the date and LocalTime… May 16, 2026 at 4:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

is there a way how to fully load some SQLAlchemy ORM mapped instance (together
The following query returns data right away: SELECT time, value from data order by
Is there way to capture and print out all of the requests and responses
Is there way to make custom signal when ManyToMany relations created?
Is there way that I can read the file from remote server using fopen
Is there way that a WCF duplex connection can be consumed in iPhone? Best
SQLAlchemy's DateTime type allows for a timezone=True argument to save a non-naive datetime object
While using SQLAlchemy, i add a object to a session using session.add(objname), then either
I'm using SQLAlchemy 0.5rc, and I'd like to add an automatic filter to a
I'm using sqlalchemy 6.0. The SQL Server T-SQL dialect seems to want to make

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.