On Dec 27, 2007, at 4:11 AM, Denis S. Otkidach wrote:

>
> On Dec 26, 2007 10:38 PM, Michael Bayer <[EMAIL PROTECTED]>  
> wrote:
>> if you have an instance which you are unsure if it already exists,  
>> you
>> can add it to a session using session.save_or_update(instance).  The
>> decision between INSERT and UPDATE is ultimately decided by the
>> presence of an attribute on the instance called "_instance_key".
>
> I'd like mapper to use UPDATE for newly constructed object (i.e.
> object without _instance_key attribute) when a record with the same
> primary key already exists in DB.

the advised way to do this is to just GET it from the database first:

rec = sess.query(MyClass).get(2)

that way you have the existing state of the row ready.  this is how  
most ORMs work.  if you just want to issue UPDATEs, you might be  
better off just using table.update() constructs to bypass the ORM.


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

if you need to ensure that id=1 exists, then within the ORM using  
query.get(id) first to retrieve the instance is the most appropriate  
strategy here.  without using the ORM its equivalent to issuing a  
SELECT for the row first, then an INSERT or UPDATE based on the results.


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