On Jan 11, 2014, at 2:20 PM, [email protected] wrote: > Wow, thanks a ton. So if I get you right, I'd register for > insert/update/delete events on the engine pointing at the database of > interest and then in my callbacks I'd have to first work out the type of the > object that was changed and then take appropriate action. Correct? > > If so, this will absolutely solve my problem.
oh, well it depends on what kind of event you’re listening for - I was
referring specifically to the “execute()” event which is a connection/engine
level event. If you’re talking about an ORM level MapperEvent
before_update/before_insert/before_delete, the “Engine” is not the target, the
mapped class is. You'd need to check on the connection within the event:
@event.listens_for(MyMappedClass, “before_insert”)
def before_insert_db1(mapper, connection, target):
if connection.engine is db1:
# do the thing
else:
# don’t do the thing
>
>
> E
>
> Sent from my BlackBerry 10 smartphone on the Rogers network.
> From: Michael Bayer
> Sent: Saturday, January 11, 2014 1:22 PM
> To: [email protected]
> Reply To: [email protected]
> Subject: Re: [sqlalchemy] Multiple databases and event.listen?
>
>
> On Jan 11, 2014, at 1:11 PM, Ed Willis <[email protected]> wrote:
>
>> Say I've got more than one database (in my specific example, they're sqlite3
>> databases). All the databases have the same table declarations in their
>> schemas. I want to listen for insert/update/delete events in one specific
>> database and then propagate data from that database to the others. What
>> I've seen in SQLAlchemy 0.7 (we're a ways back, yes) is that there doesn't
>> seem to be an easy way for me to either filter out the event notifications
>> to just the one databases of interest OTOH or work out which database the
>> data came from when the event is triggered OTO.
>>
>> Does anyone know of an easy way to limit event notifications to just one
>> database?
>
> event.listen() accepts an Engine instance, not just a class, as a target:
>
> db1 = create_engine(..)
> db2 = create_engine(..)
> db3 = create_engine(..)
>
> @event.listens_for(db1, “execute”)
> def only_db1_events(…):
> …
>
> more info: http://docs.sqlalchemy.org/en/rel_0_9/core/event.html#targets
>
>
> hope this helps
>
>
>
>>
>> Thanks!
>>
>> --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/sqlalchemy.
>> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
signature.asc
Description: Message signed with OpenPGP using GPGMail
