Hi Simon, I want to specify the "current user_id" with some kind of filter, so I either get thing+like or thing (if no like is found).
The joinedload does what I need, but it still needs the filtering (so I don't get all likes for a product, but only 1 filtered by user_id) ? On Thu, Nov 10, 2016 at 11:53 AM, Simon King <si...@simonking.org.uk> wrote: > On Thu, Nov 10, 2016 at 9:32 AM, Dorian Hoxha <dorian.ho...@gmail.com> > wrote: > > So I have: > > > > class Thing(): > > current_user_like = relationship(Like) > > > > class User(): > > pass > > > > class Like(): > > user_id = Column(primary_key=True) > > thing_id = Column(primary_key=True) > > > > > > Now I want, to do "session.query(Thing).all()" and also join the > > "current_user_like" for each Thing, but only get 1 row instead of the > whole > > collection of `likes` for each Thing. > > I know this can be done with db.session.query(Thing,Like) and doing an > > outerjoin, but can it be done so I get Like embedded inside > > Thing.current_user_like ? > > I'm not sure I understand the question. How do you specify which > user_id should correspond to the "current user"? Are you asking how to > create the "current_user_like" relationship, or how to populate it > during a query? > > In general, if you want a relationship to be populated during a query, > you use one of the loader options such as joinedload or subqueryload: > > http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#using- > loader-strategies-lazy-loading-eager-loading > > 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 a topic in the > Google Groups "sqlalchemy" group. > To unsubscribe from this topic, visit https://groups.google.com/d/ > topic/sqlalchemy/pRdF3mRkaEE/unsubscribe. > To unsubscribe from this group and all its topics, 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. > -- 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.