Jyotirmoy wrote:
>
> I want to know what is the best way to write code using sqlalchemy
> that given a value of the primary key inserts a record if no record
> with that key exists or updates the value of the other columns if the
> key already exists.
>
> I wrote a naive version which get()s the value and add()s if the
> result is None. But the approach fails with many concurrent processes
> since the record may have been inserted between the get and add.

 I
> could catch the IntegrityError and retry, but if there are other
> processes deleting records as well then there is no guarantee that the
> loop would ever terminate.
>
> Is there some approach to doing this operation which is guaranteed to
> succeed.


you might want to try using table locks.  the MySQL documentation should
have some pointers on how to do this.   also definitely be on InnoDB
tables for the best transaction isolation behavior.


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