On Sun, Sep 23, 2018 at 12:20 AM Colton Allen <cmanalle...@gmail.com> wrote:
>
> As the title says, I'd like to filter a related table without joining the 
> table (I don't want to mess up my pagination).  Is there a way to enforce the 
> from clause?  My SQL is a bit rusty but I'm pretty sure its possible.
>
> On SQLAlchemy==1.2.12:
>
> from sqlalchemy.ext.declarative import declarative_base
>
> import sqlalchemy as sa
>
>
> Model = declarative_base()
>
>
> class Person(Model):
>     __tablename__ = 'person'
>
>     id = sa.Column(sa.Integer, primary_key=True)
>
>
> class Note(Model):
>     __tablename__ = 'note'
>
>     id = sa.Column(sa.Integer, primary_key=True)
>     person_id = sa.Column(sa.Integer, sa.ForeignKey('person.id'))
>
>     person = sa.orm.relationship('Person', backref='notes')
>
>
> engine = sa.create_engine('sqlite://')
> Model.metadata.create_all(engine)
>
>
> session_cls = sa.orm.sessionmaker(bind=engine)
> session = session_cls()
>
>
> person_query = session.query(Person).filter(Person.id == Note.person_id, 
> Note.id == 1)
> print(person_query)
>
> "person_query" is:
>
> SELECT person.id AS person_id
> FROM person, note
> WHERE person.id = note.person_id AND note.id = ?

Can you explain what you mean by "enforce the FROM clause"? As far as
I can tell, your "person_query" is correct, so I'm not sure what you
are looking for.

Thanks,

Simon

-- 
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