On Oct 17, 2013, at 1:48 AM, Christoph Reisbach <[email protected]> 
wrote:

> 
> existing_query = session.query(TableOne).subquery()
> 
> additional_query = session.query(TableTwo).subquery()
> 
> combined_query = session.query(existing_query).join(additional_query)
> print combined_query.all()[0]
> # out: (1, u'some name')
> 
> # -------------------------------
> 
> combined_query = session \
>         .query(TableOne) \
>         .select_from(combined_query.subquery().select())
> 
> result = combined_query.all()
> 
> print combined_query.all()[0]
> print combined_query
> # out: <__main__.TableOne object at 0x13224d0>
> --------------------------------------------------------------------------------------------------------------------------
> 
> The 'First Query' looks like this.
> 
> SELECT anon_1.id AS anon_1_id, anon_1.name AS anon_1_name 
> FROM (
>     SELECT table_one.id AS id, table_one.name AS name 
>     FROM table_one) AS anon_1 
> JOIN (
>     SELECT table_two.id AS id, table_two.f_id AS f_id, table_two.description 
> AS description 
>     FROM table_two) AS anon_2 
> ON anon_1.id = anon_2.f_id
> 
> 
> That is also what I what, but it returns the list of values.
> 

well you can get both conditions by just not using the first subquery (it's how 
I'd do it....)

addtl_query = session.query(TableTwo).subquery()   # assuming this even needs 
to be a subquery
session.query(TableOne).join(addtl_query).all()

> As you see it contains some  unnecessary statements,
> but it returns a list of objects (TableOne) that I need.
> 
> What is the correct way to join two subqueries?
> 
if you truly have to keep both subqueries and then return entities, 
select_from() is the normal way to do it - it is always going to re-state the 
subquery in terms of the columns it needs however.  If you think you can load 
straight from your subquery you can try using from_statement() instead.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to