> > Can someone help me to get this right?
>
> this is ticket 1362 and is a
> TODO:http://www.sqlalchemy.org/trac/ticket/1362.

My fault, I should have checked the tickets before posting.

> For now you need to issue update() statements for the tables
> manually,then reload your objects.
>

Waiting for the new release with the issue fixed might not be an
option (unless it is planed very soon:). However, manually updating
tables at different locations in the code does not seem a good
alternative either.

For that reason I considered to update the table in a
SessionExtension. The extension, as shown below, updates the table at
the database level before a flush(). As I understand the documentation
a refresh reloads the object incorporating the changes. Now if I
continue to work with the object instance, I get an
ObjectDeletedError. Could you once more help me to understand what is
going on and how I can fix this?

Thanks a lot.


class UpdateExt(SessionExtension):
    def before_flush(self, session, flush_context,instances):
        for employee in session.dirty:
            if isinstance(employee, Employee):
                table = Table('employees', base.metadata,
                              autoload=True)
                upd = table.update(values={'name': employee.name})
                base.metadata.bind.execute(upd)
                session.refresh(employee, ['name'])

#============================
Session = sessionmaker(extension=UpdateExt())
session = Session()
joe = Engineer('Joe','Engineer of the month')
session.add(joe)
session.commit()
joe.name = 'Joey'
session.commit()  #Changes persistent in the database, no Error
print joe.name    #ObjectDeletedError


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to