Hello guys!


I am trying to join two models by relationship, but I want to emulate 
values of second model using from_statement.


class X(Base):
    __tablename__ = 'x'
    id = Column(Integer, primary_key=True)
class Y(Base):
    __tablename__ = 'y'
    id = Column(Integer, primary_key=True)
    x_id = Column(Integer, ForeignKey(X.id))
    x = relationship(X)
Y1 = aliased(Y, name='y1')X1 = Query(X).from_statement(select([label('id', 
literal(5))])).subquery(name='x1')session.query(Y1).join(X1, Y.x).all()


Here 
<https://bitbucket.org/zzzeek/sqlalchemy/issues/4274/join-queryfrom_statement#comment-46063178>
 Michael 
Bayer suggested to do the following:


Y1 = aliased(Y, name=‘y1’)
stmt = select([label(‘id’, literal(5))])
X1 = Query(X).from_statement(stmt).subquery(name=‘x1’)
session.query(Y1).join(X1, stmt.alias(“x1”).c.id == Y1.id).all()


But in that case I would need to convert relationship Y.x to corresponding 
ON clause.

Is it possible to do that automatically? I.e. For any relationship derive 
corresponding ON clause?


Thanks in advance!


-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to