Re: [sqlalchemy] Self referential table, with composite key, and relationship(..., lazy='joined') is not eager loading as expected.
I'm going to link the docs for easy reference for any future readers: http://docs.sqlalchemy.org/en/latest/orm/self_referential.html?highlight=join_depth#configuring-self-referential-eager-loading I ended up using something link session.query(Game).options(joinedload(Game.opp)) because I didn't want to settle on one specific join_depth. Thank you for your suggestion. I was able to solve my issue because of fit. I am very happy with the abstractions available in SQLAlchemy to help with eager loading. -- 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.
Re: [sqlalchemy] Self referential table, with composite key, and relationship(..., lazy='joined') is not eager loading as expected.
Self-referential lazy="joined" requires the join_depth argument. Check the docs. > On Mar 12, 2016, at 6:22 PM, Devin Jacobswrote: > > from sqlalchemy import Column, and_ > from sqlalchemy import create_engine, Integer, String, Date, Enum > from sqlalchemy.orm import sessionmaker, relationship, remote, foreign > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy.exc import DataError, IntegrityError > > > Base = declarative_base() > > > class Game(Base): > __tablename__ = 'game' > > team = Column(String, primary_key=True, index=True) > opponent = Column(String, primary_key=True, index=True) > date = Column(Date, primary_key=True, index=True) > result = Column(Enum('win', 'loss', name='win_loss'), index=True) > points = Column(Integer) > opp = relationship( > 'Game', > uselist=False, > lazy='joined', > primaryjoin=and_( > foreign(team) == remote(opponent), > foreign(opponent) == remote(team), > foreign(date) == remote(date))) > > > engine = create_engine('postgresql://buttons:buttons@localhost/ncaa', > echo=True) > new_session = sessionmaker(engine) > > > # The following line produces a query like: select * from game > # The query does NOT contain a join. > new_session().query(Game).all() > > The query on the last line off the example produces a simple "select * from > game" type query (the columns are spelled out of course). I was expecting the > lazy='joined' argument to result in a join. What am I doing wrong? > -- > 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. -- 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.
[sqlalchemy] Self referential table, with composite key, and relationship(..., lazy='joined') is not eager loading as expected.
from sqlalchemy import Column, and_ from sqlalchemy import create_engine, Integer, String, Date, Enum from sqlalchemy.orm import sessionmaker, relationship, remote, foreign from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.exc import DataError, IntegrityError Base = declarative_base() class Game(Base): __tablename__ = 'game' team = Column(String, primary_key=True, index=True) opponent = Column(String, primary_key=True, index=True) date = Column(Date, primary_key=True, index=True) result = Column(Enum('win', 'loss', name='win_loss'), index=True) points = Column(Integer) opp = relationship( 'Game', uselist=False, lazy='joined', primaryjoin=and_( foreign(team) == remote(opponent), foreign(opponent) == remote(team), foreign(date) == remote(date))) engine = create_engine('postgresql://buttons:buttons@localhost/ncaa', echo=True) new_session = sessionmaker(engine) # The following line produces a query like: select * from game # The query does NOT contain a join. new_session().query(Game).all() The query on the last line off the example produces a simple "select * from game" type query (the columns are spelled out of course). I was expecting the lazy='joined' argument to result in a join. What am I doing wrong? -- 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.