With loader criteria and baked query cache

25 views
Skip to first unread message

Tomas Pavlovsky

unread,
Jan 7, 2022, 12:51:57 PM1/7/22
to sqlalchemy
Hello Mike, 


class X()
   id: id: int = Column( Integer, primary_key=True)
   a_id:int = Column(Integer, ForeignKey("A.id"))

subq = select(A_rel_B).join(B).order(B.size).limit(10)

class A()
   id: id: int = Column( Integer, primary_key=True)
   bs = relationship(B, viewonly=True, secondary=subq, uselist=False,primaryjoin=and_(A.id == subq.c.a_id))

class B()
   id: id: int = Column( Integer, primary_key=True)
   size= Column( Integer)

class A_rel_B()
  a_id: :int = Column(Integer, ForeignKey("A.id"))
  b_id: :int = Column(Integer, ForeignKey("B.id"))

 x: int 
q = select(X).options(
   selectinload(A).options(joinedload(A.bs)),
with_loader_criteria(A_rel_B, func.fn(A_rel_B.b_id, x).is_(True))
)

When i add with_loader_criteria(A_rel_B, func.fn(A_rel_B.b_id, x).is_(True)), then x value is cached and don't change. It is ok?

Thanks

Mike Bayer

unread,
Jan 7, 2022, 2:15:32 PM1/7/22
to noreply-spamdigest via sqlalchemy
i dont think with_loader_criteria ever expected the target entity to be part of "secondary" in a relationship().  I have no idea what that would do and I'm surprised it works at all.

I would try instead to map from A->A_rel_B->B explicitly and use two relationships for this join.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
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.

Reply all
Reply to author
Forward
0 new messages