I'm using SQLAlchemy 0.4.6 with Python 2.5.2, and having a problem
with deleting object. Below is the testing code that shows my problem:

from sqlalchemy import *
from sqlalchemy.orm import *

engine = create_engine('sqlite:///C:/test_db.sqlite')
Session = sessionmaker(bind=engine, autoflush=True,
transactional=True)

metadata = MetaData()


class Foo(object):

    def __init__(self, name):
        self.name = name


foo_table = Table('foo', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(40))
)


if __name__ == '__main__':
    # Create database, map database table to its model
    metadata.create_all(engine)
    mapper(Foo, foo_table)

    # 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)

I got a traceback below:

f = <__main__.Foo object at 0x00E64FD0>
f1 = <__main__.Foo object at 0x00E6D8D0>
f1 is f = False
Traceback (most recent call last):
  File "test_sqlalchemy.py", line 39, in <module>
    db_session2.delete(f)
  File "C:\Python25\lib\site-packages\SQLAlchemy-0.4.6-py2.5.egg
\sqlalchemy\orm\
session.py", line 954, in delete
    self._delete_impl(instance)
  File "C:\Python25\lib\site-packages\SQLAlchemy-0.4.6-py2.5.egg
\sqlalchemy\orm\
session.py", line 1123, in _delete_impl
    raise exceptions.InvalidRequestError("Instance '%s' is with key %s
already p
ersisted with a different identity" %
(mapperutil.instance_str(instance), instan
ce._instance_key))
sqlalchemy.exceptions.InvalidRequestError: Instance '[EMAIL PROTECTED]' is
with key (
<class '__main__.Foo'>, (1,), None) already persisted with a different
identity

It seems that when I get the object back from db with a different
session, new instance of the same db record is created in this
session. There are 2 different objects presenting the same  record in
2 different sessions, and SQLAlchemy is not aware of this.

Any help or idea is appreciated!!!

--~--~---------~--~----~------------~-------~--~----~
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