You can do something like:
q1 = session.query(\
ContactInfo.account_id.label("q1_account_id" ),
ContactInfo.other_id.label("q1_otherid" )
)\
.filter(..)
q2 = session.quiery(Site.account_id.label("q2_account_id")).filter(..)
_q3 = sqlalchemy.union( q1 , q2 )
q3 = sqlalchemy.sql.expression.alias( _q3 , name="unioned")
q = sqlalchemy.select(\
(\
q3.c.q1_account_id.label('contact_account_id') ,
q3.c.q2_account_id.label('site_account_id') ) ,
),
)\
.filter()\
.order_by()
results = dbSession.execute( q )
the thing to note on this approach ( which works great ) is that you can no
longer use .all()
if you conditionally generate the joins...
if isinstance( _query , sqlalchemy.orm.query.Query ):
results = _query.all()
else:
results = dbSession.execute( _query )
hope that helps!
there are probably other ways to do it, but this approach works for me
--
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/groups/opt_out.