On Oct 9, 2008, at 8:40 PM, Moshe C. wrote:

>
> Let's say you have two concurrent processes where each might increment
> some integer field in some table row.
> If you query first and the increment in memory and then update, you
> need to query with_lockmode('update') to avoid the case where both
> processes read the same value and the do the same increment.
> A simpler method is to use SQL s.a. set field = field+1.
>
> Q: How do you generate such SQL with SA ?

with expression language:

engine.execute(table.update().values(field = table.c.field + 1))

with ORM:

myobject.field = MyClass.field + 1
session.flush()

> Is there a better method to do it?

the "atomic" increment is probably fine unless you need the value  
beforehand, sequences provide a nice way to get an "atomic"  
incrementing value, if your DB provides them.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
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