I would like to increase (or decrease) the rating field in an Elixir object:
class Posting(Entity): score = Field(Integer, PassiveDefault(text('0'))) def upvote(self): self.score = self.score + 1
However, this does not work reliably with simultaneous calls for promotion. The best I could come up with is an ugly mess (basically building SQL UPDATE with SQLAlchemy):
def upvote(self):
Do you see any problems with this solution? Are there any cleaner ways to achieve the same?
I would like to avoid using DB locks here. I use Elixir, SQLAlchemy, Postgres.
Update
Here is an option that is deduced from the vonPetrushev solution:
def upvote(self): Posting.query.filter_by(id=self.id).update( {Posting.score: Posting.score + 1} )
This is slightly better than my first solution, but filtering for the current object is still required. Unfortunately, this does not work if Entity is spread across multiple tables.
source share