Tai Tran wrote:
>
> [snip]
>
> class PC(object):
>
> ...
>
> def destroySelf(self):
> db_session = Session()
> ...
> for port in self.ports:
> port.destroySelf()
> ...
> db_session.delete(self)
>
> ...
>
>
> class Port(object):
>
> ...
>
> def destroySelf(self):
> db_session = Session()
> ...
> db_session.delete(self)
>
>
> I'm using contextual/thread-local sessions, in the last statement
> "db_session.delete(self)" of Port.destroySelf(), I always get the same
> traceback as I tried to demonstrate in the last port:
>
> "sqlalchemy.exceptions.InvalidRequestError: Instance
> '[EMAIL PROTECTED]' is with
> key (<class '__main__.Port'>, (2,), None) already persisted
> with a different
> identity"
>
> I found a solution to solve this problem by deleting the
> object in current
> session:
>
> def destroySelf(self):
> db_session = Session()
> ...
> obj = db_session.get(Port, self.id)
> db_session.delete(obj)
>
> But I'm wondering whether it is the right approach?
>
The 'object_session' function returns the session that an object is
currently associated with. I would have thought that you could write
your destroySelf method as:
def destroySelf(self):
db_session = object_session(self)
db_session.delete(self)
Actually, looking at the 0.5 docs, I see that object_session is now a
classmethod on Session, so perhaps it should be written like this:
def destroySelf(self):
db_session = Session.object_session(self)
db_session.delete(self)
See 'How can I get the Session for a certain object' in the FAQ section
of http://www.sqlalchemy.org/docs/05/session.html
Hope that helps,
Simon
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---