The code looks fine to me, other than the access of Session._new for which you 
should be really calling upon the public "new" collection.

What you might want to make sure of is that the objects you expect to see in 
"dirty" are actually there.   Sometimes objects don't make it into "dirty" 
until they are found to be impacted by a relationship() changing something on 
them.   If this is the case you'd want to navigate to those objects differently.


On Jun 6, 2012, at 6:36 AM, Damian Dimmich wrote:

> Hi,
> 
> I have a before flush event set up that sets the current user_id and datetime 
> on new objects that are going to be commited to the database (all tables have 
> these fields).
> 
> Unfortunately, it seems to ignore changes I make to existing objects, ie when 
> I set the user_id (which i get from pyramid) and last_updated columns in the 
> event handler, the sql only issues updates for the other columns that where 
> modified.
> 
> Any ideas as to what am I missing?
> 
> Thanks in advance,
> Damian
> 
> 
> def attach_user_committing(Session, flush_context, instances):
>    """This function gets called by the before_flush event,
>    it grabs the current threads request and extracts the
>    authenticated user_id out of it.  You can only
>    commit things if you are authenticated.
> 
>    Once it has that id, it goes through and adds the id
>    of the user who modified it to new commits- need to be careful that
>    _new doesn't change.
>    """
> 
>    #this is used when creating databases & testing only
>    user_id = 1
>    if not creating_database:
>        user_id = 
> authenticated_userid(pyramid.threadlocal.get_current_request())
>    #for each object being committed/flushed, set the flushing/commiting user
>    for obj in Session._new.values():
>        #log.debug(obj)
>        obj.user_id = user_id
>        obj.last_updated = datetime.now()
>    log.debug('Session dirty is : %s' %Session.dirty)
>    for obj in Session.dirty:
>        log.debug('Objects in dirty: %s' % obj.__dict__)
>        #if obj._sa_instance_state.modified:
>        obj.user_id = user_id
>        obj.last_updated = datetime.now()
> 
> 
>    #session.user_id = request
> #this event ensures that user_id & lastupdate is correctly stored on each 
> commit.
> event.listen(Session, "before_flush", attach_user_committing)
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> 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.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
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