@event.listens_for(Query, "before_compile", retval=True)
def before_compile(query):
print("XXXXX before_compiled called XXXXX")
return query
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
name = Column(String)
addresses = relationship("Address", back_populates="user")
class Address(Base):
__tablename__ = "address"
id = Column(Integer, primary_key=True)
email = Column(String)
user_id = Column(Integer, ForeignKey("user.id"))
user = relationship("User", back_populates="addresses")
and then I do the following twice:
for u in Session().query(User):
for a in u.addresses:
print(u.name, a.email)
The first time before_compile is called twice, one call for the SELECT FROM "user" query, and another call for the SELECT FROM "address" query. But the second time before_compile is called only once. It's called for the SELECT FROM "user" query, but not for the SELECT FROM "addresses" query. Is it expected?
I am using SQLAlchemy 1.3.10.
The full test case is here: https://gist.github.com/elemoine/3fa86da54fc1195e314fa18999d05a68
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/ef750756-4a8e-4043-bd5e-873a188612ec%40googlegroups.com.
The first time before_compile is called twice, one call for the SELECT FROM "user" query, and another call for the SELECT FROM "address" query. But the second time before_compile is called only once. It's called for the SELECT FROM "user" query, but not for the SELECT FROM "addresses" query. Is it expected?
I am using SQLAlchemy 1.3.10.sure, it sounds like u.addresses is already loaded. if you want u.addresses to emit a query every time it is called you need to use the "dynamic" loader strategy, that is, lazy="dynamic".
Thanks for your response.
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/6ff6bf6c-0fa6-4795-a6f7-0b35d9417a08%40googlegroups.com.
On Tue, Oct 22, 2019, at 11:58 AM, Eric Lemoine wrote:The first time before_compile is called twice, one call for the SELECT FROM "user" query, and another call for the SELECT FROM "address" query. But the second time before_compile is called only once. It's called for the SELECT FROM "user" query, but not for the SELECT FROM "addresses" query. Is it expected?
I am using SQLAlchemy 1.3.10.sure, it sounds like u.addresses is already loaded. if you want u.addresses to emit a query every time it is called you need to use the "dynamic" loader strategy, that is, lazy="dynamic".I think the problem is related to "baked queries". I don't have the problem with enable_baked_queries set to False in sessionmaker.oh right that too, those queries are cached. but whatever changes you made to the query should also be cached, how come you need it to be different every time?
Thanks Mike.--Eric
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAEE1YngiAsqqUwFJAaHZC0pj%3DU0pUtfGtEA2UPxv0toaCNcEwg%40mail.gmail.com.
it sounds like you should use a bound parameter with a lambda inside of it, there's not an explicit "on lazyload" hook at the moment.
it sounds like you should use a bound parameter with a lambda inside of it, there's not an explicit "on lazyload" hook at the moment.Yep, that seems to be doing the job!
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAEE1Yngbpq8s4EoJOoLQ07t852VLK0-5RGMQuzM7rREBSaGBjA%40mail.gmail.com.
On Tue, Oct 22, 2019, at 12:30 PM, eric.l...@gmail.com wrote:it sounds like you should use a bound parameter with a lambda inside of it, there's not an explicit "on lazyload" hook at the moment.Yep, that seems to be doing the job!that worked? wow:)
guess you are using a threadlocal to get the context?
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAEE1YnjRsi9kh-V3NtVUQL--TqbCaGPCj4V%3Drj8v2sc8bq%3DU%3Dg%40mail.gmail.com.
today, we have this issue, which I can confirm has existed since version 1.2 over two years ago, reported for the first time ever here, and then a day later in https://github.com/sqlalchemy/sqlalchemy/issues/4947 .