On Fri, Oct 20, 2023, at 8:08 AM, 'Tony Cosentini' via sqlalchemy wrote:
> Hi,
> 
> Is there any way to listen for an event for when a query result gets merged 
> into a pre-existing object in the session?

this is the refresh event:

https://docs.sqlalchemy.org/en/20/orm/events.html#sqlalchemy.orm.InstanceEvents.refresh


> 
> I'm working on disabling autoflush for some of our codebase (mostly to cut 
> down on queries on a high QPS path), but before doing that I want to write a 
> utility to detect when we read data that was flushed via autoflush to cut 
> down on any potential bugs.
> 
> What I'd like to do is this (while autoflush is still enabled):
> 
> table_one = TableOne(name='test')
> session.add(table_one) # Track this object as pending a flush via the 
> before_* listeners, this is working as expected.
> 
> table_two_instances = session.query(TableTwo).all() # All good, doesn't do 
> anything with the table_one instance created earlier
> 
> table_one_instances = session.query(TableOne).all() # I would like to log a 
> warning here as the results of this query depend on a flush happening. What 
> I'm hoping to do is detect that one of the rows coming back is the same 
> object that was flagged earlier, but I can't see to find the correct event to 
> use.

you probably want to also use refresh_flush also, which will be invoked for 
column defaults that are populated on the object

https://docs.sqlalchemy.org/en/20/orm/events.html#sqlalchemy.orm.InstanceEvents.refresh_flush




> 
> Is this possible? I can't seem to find the appropriate event that would 
> trigger when the results from the .all() query get merged back in with the 
> existing objects in the session.

that's the refresh event

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/c5b89f67-e3fa-45f2-8774-463d154514f3%40app.fastmail.com.

Reply via email to