Thanks a lot for your help!

My example code is just a simplified version of the real problem I'm having
when porting my application from SQLObject to SQLAlchemy. In my application,
each model class has a method named destroySelf(). The main purpose of this
one is to delete current object in the database, and also clean up other
things like removing related records.

Say I have 2 related model classes: PC and Port, one PC has many ports
(one-to-many relationship). In PC, .ports is the attribute used to access
its related port objects (assume all table definitions and mappers have been
set correctly). the definition of destroySelf() of PC and Port classes are:

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?

-----Original Message-----
From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Michael Bayer
Sent: Monday, June 30, 2008 10:54 PM
To: sqlalchemy@googlegroups.com
Subject: [sqlalchemy] Re: Cannot delete persisted object which is not in
current session



On Jun 30, 2008, at 4:37 AM, Tai Tran wrote:

>    # Create a new Foo object, then save it to the database
>    db_session = Session()
>    f = Foo('001')
>    db_session.save(f)
>    db_session.commit()
>
>    # Try deleting Foo object created above with a new, different
> session
>    db_session2 = Session()
>    f1 = db_session2.get(Foo, f.id)
>    print 'f =', f
>    print 'f1 =', f1
>    print 'f1 is f =', f1 is f
>    db_session2.delete(f)
>

"f" is still attached to db_session.  if you want to delete "f1", say  
db_session2.delete(f1).  Or if "f", say db_session.delete(f).   if you  
want to move objects between sessions, use session.expunge()/ 
session.save_or_update().





--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to