Hi,

I have the following question: if an object has been deleted from a
database using session.delete(), which method of the Session object
should be used to add that object back to the database? I thought
merge() should do the trick, but it doesn't work. For example, when I
invoke the following code (which is supposed to simulate a sequence of
Do/Undo/Redo/Undo operations)

from sqlalchemy import *
from sqlalchemy.orm import *

class Object(object): pass

db = create_engine("sqlite:///:memory:")
metadata = MetaData(db)

objects_tbl = Table("objects", metadata,
                    Column("id", Integer, primary_key=True),
                    Column("name", String()))
objects_tbl.create()

mapper(Object, objects_tbl)
session = create_session()
metadata.engine.echo = True

# Create object
obj = Object()
obj.name = "My name"

# "Do": save object
print "\nsave\n"
session.save(obj)
session.flush()

# "Undo #1": delete object
print "\nundo #1\n"
session.delete(obj)
session.flush()

# "Redo": merge object?
print "\nredo\n"
session.merge(obj) # ERROR HERE
session.flush()

# "Undo #2": delete object
print "\nundo #2\n"
session.delete(obj)
session.flush()

I get the following error:

Traceback (most recent call last):
  File "test_case.py", line 34, in ?
    session.merge(obj)
  File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.3.10-py2.4.egg/
sqlalchemy/orm/session.py", line 501, in merge
    raise exceptions.AssertionError("Instance %s has an instance key
but is not persisted" % mapperutil.instance_str(object))
NameError: global name 'mapperutil' is not defined

I have also tried the update() method, but apparently in this case it
doesn't do anything (no SQL code is produced) so that  the second
delete() call fails, as the code is trying to delete an already
deleted object. Actually, it's not clear to me in what state the
delete() method leaves the object: I thought it was "detached", but
then I suppose the update() method should work...

By the way, it looks that there's a bug in the session.py file: the
"from sqlalchemy.orm import util as mapperutil" line is missing.

Best regards,
Wojciech


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