Hi,
I'm trying to use events to determine which instances were updated in the 
db, and how.  

I'd like to know:
1. instances that had column data updated
2. instances that had relationships updated
3. instances that were deleted
4. instances that were inserted

I think I have a handle on 1-3, but I can't figure out how to tell if an 
instance was newly inserted.

Here's some sample 
code: https://gist.github.com/chadrik/08617b6ed4d9cbacd93c

The core of it looks like this:

@event.listens_for(Session, 'after_flush_postexec')
def after_flush_postexec(session, flush_context):
    print "-" * 40
        for state in flush_context.states:
        print state.object
 
        for at in state.attrs:
            history = flush_context.get_attribute_history(state, at.key)
            added, unchanged, deleted = history
 
            if added:
                print " added:", at.key, [x.object for x in added]
 
def test():
    Base.metadata.create_all(engine)
 
    book = Book(title='sqlalchemy for dummies')
    session = Session()
    session.add(book)
    session.commit()
 
    john = Author(name='John Doe')
    jane = Author(name='Jane Doe')
    book.authors = [john, jane]
    book2 = Book(title='sqlalchemy pro tips')
    book2.authors = [john]
    session.add(book2)
    session.commit()

this prints:

----------------------------------------
Book(1)
----------------------------------------
Book(2)
 added: authors [Author(1)]
Author(2)
 added: books [Book(1)]
Book(1)
 added: authors [Author(1), Author(2)]
Author(1)
 added: books [Book(1), Book(2)]


when the event runs after the second commit, how can I tell that Book(2) 
was inserted and Book(1) was not?

thanks!
chad.




-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to