In the MySQL system I'm redoing with SQLAlchemy, there is effectively a 
"master' schema that exists once top-level stuff (including a table 
describing projects) , and "project" schemas (one identical set of tables 
per project), which have project-level stuff.  When an object is retrieved 
from a project, there is nothing in the data itself which indicates which 
project it was loaded from, but this may in certain cases be needed.  I 
have a nice collection of Project objects loaded from the master schema 
lying around, and I pass my own subclass of SQLA's Session to 
sessionmaker() so I can store a reference to the appropriate Project with a 
session.  But since Detached and Transient instances don't have an 
associated session, I can't just use object_session() to access the Project 
via the instance's session.  So I thought I'd stamp a reference to the 
appropriate Project onto instances.

SQLA's event system has the "after_attach" session event.  Hooking into 
this works for new instances I attach to a session with add(), but does't 
fire when items are loaded from a query - presumably "attached" means 
direct userland attachment only, not instances being associated with a 
session via a query. So it's only half the solution - there doesn't seem to 
be a session-level  event that fires in the latter case?

There is a "load" instance-level event, which seems to solve the other half 
of this.  The QueryContext passed to the handler contains the session, and 
I can stamp the instance with the Project reference I've stored in the 
session.  The only drawback is that I apparently need to separately attach 
the event listener to each project instance class?  Presumably I could 
somehow retrieve a list of classes from the declarative_base 
(_decl_class_registry_ ?) and walk through that adding the listeners.

Is this a reasonable approach, and between the two events I'll be seeing 
all the objects that enter the session either from database retrieval or my 
code?


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/_F71NtWi9MsJ.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to