On Jan 11, 2008, at 12:30 PM, Denis S. Otkidach wrote:

>
> # Another program. We have to insure that object with id=1 exists in  
> DB and has
> # certain properties.
> obj2 = ModelObject(1, u'title2')
> session.merge(obj2)
> session.commit()

what that looks like to me is that you're attempting to query the  
database for object ID #1 using merge().

when you merge(), its going to treat the object similarly to how it  
does using session.save_or_update().  that is, it looks for an  
"_instance_key" attribute to determine if the object represents a  
transient or persisted instance.

So you could "hack" the way youre doing it like:

obj2 = ModelObject(1, u'title2')
obj2._instance_key = session.identity_key(instance=obj2)
session.merge(obj2)
session.commit()

we have yet to define a completely public API for the above operation,  
i.e. "treat this object as though its persistent".  im not sure yet  
how we could define one that has a straightforward use case which  
wouldn't add confusion.

Anyway, the "legit" way to go is this (and this is what the above  
merge() is doing anyway):

obj2 = session.query(ModelObject).get(1)
if not obj2:
        obj2 = ModelObject(1, u'title2')
        session.save(obj2)
else:
        obj2.title= u'title2'
session.commit()





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