On Oct 17, 2013, at 11:21 AM, Chris <christoph.reisb...@gmail.com> wrote:

> They both have to be subqueries because they can have where-statements.
> If select_from() is the normal way than I have to live with these additional 
> select-from-constructions.
> 
> Do you have/know some example of how to use from_statement()?
> Because I tried it with from_statement() but it doesn't work.
> 
> 
> combined_query = 
> session.query(existing_query).join(additional_query).subquery()
> session.query(TableOne).from_statement(combined_query.statement)
> 
> --> raises NoSuchColumnError: "Could not locate column in row for column 
> 'table_one.id'

I'm able to get a pretty clean statement by applying the join after the 
select_entity_from()  (note that select_entity_from and select_from are the 
same in 0.8, but in 0.9 you'll want to use select_entity_from()):

q1 = sess.query(A).subquery()
q2 = sess.query(B).subquery()

sess.query(A).select_entity_from(q1).join(q2).all()

output:

SELECT anon_1.id AS anon_1_id 
FROM (SELECT a.id AS id 
FROM a) AS anon_1 JOIN (SELECT b.id AS id, b.a_id AS a_id 
FROM b) AS anon_2 ON anon_1.id = anon_2.a_id

> 
> By the way is there a way to retrieve the given class from a query object.
> From example:
> If a method create a Query object like this:
> query = session.query(TableOne, TableTwo).filter(...)
> and returns 'query'.
>  How can I find out that the query was created with the objects TableOne and 
> TableTwo ?

Query has an accessor "column_descriptions" : 
http://docs.sqlalchemy.org/en/rel_0_8/orm/query.html?highlight=column_descriptions#sqlalchemy.orm.query.Query.column_descriptions


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

Reply via email to