there's no joins in the statement you describe, you can get that
close-to-identical SQL using union() and exists() normally, full
example:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
bs = relationship("B")
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey('
a.id'))
session = Session()
"""
I'd like to generate the following statement:
SELECT * FROM a WHERE (EXISTS
(
SELECT 1 FROM b WHERE SomeConditionOnB AND b.a_id =
a.id
UNION SELECT 1 FROM b WHERE SomeOtherConditionOnB AND b.a_id =
a.id
)
)
"""
q = session.query(A).filter(
session.query(B.id).filter(B.id > 5).filter(B.a_id == A.id).correlate(A).
union(
session.query(B.id).filter(B.id < 10).filter(B.a_id == A.id).
correlate(A)
).exists()
)
print(q)
"""
SELECT
a.id AS a_id FROM a WHERE EXISTS
(
SELECT 1 FROM (
SELECT
b.id AS b_id FROM b WHERE
b.id > :id_1 AND b.a_id =
a.id
UNION SELECT
b.id AS b_id FROM b WHERE
b.id < :id_2 AND
b.a_id =
a.id
) AS anon_1
)
"""
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
>
http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See
http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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
sqlalchemy+...@googlegroups.com.
> To post to this group, send email to
sqlal...@googlegroups.com.
> Visit this group at
https://groups.google.com/group/sqlalchemy.
> For more options, visit
https://groups.google.com/d/optout.