On Aug 7, 2013, at 11:58 AM, Etienne Rouxel <[email protected]> wrote:

> Hello
> 
> Long story cut short, I would like to know if it is possible to generate a 
> query with SQLAlchemy ORM such as the one below and how.
> 
>     SELECT botany.taxon.id AS botany_taxon_id
>     FROM botany.taxon 
>     WHERE EXISTS (SELECT 1
>         FROM botany.plant 
>         JOIN product.article ON botany.plant.id = product.article.plant_id
>         JOIN catalog.catalogitem ON product.article.plant_id = 
> catalog.catalogitem.plant_id AND product.article.article_id = 
> catalog.catalogitem.article_id
>         WHERE :param_1 = catalog.catalogitem.marketingseason_id
>         AND botany.plant.taxon_id = botany.taxon.id
>     )
> 
> Put differently, it is like the regular use of EXISTS 
> (http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html#using-exists) but 
> with a more complex subquery that contains JOINs.
> 
> Is it possible to do such a query?


Query has an exists() method that will turn the SELECT you've constructed into 
an EXISTS: 
http://docs.sqlalchemy.org/en/rel_0_8/orm/query.html?highlight=query.exists#sqlalchemy.orm.query.Query.exists
  So you say "e = query(Plant).join(..).join(..).filter(...).exists();  
query(Taxon).where(e)".

before we had that method you also could construct the joins using orm.join() 
and then use sqlalchemy.exists() to produce a select, but the Query method is a 
shortcut on that.

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

Reply via email to