Understanding and Preventing Race Conditions in Web Applications

4 pointsposted 13 hours ago
by Tiberium

1 Comments

tony-allan

12 hours ago

A mistake that I have made!

And the answer is:

  UPDATE TABLE posts
    SET views = views + 1
    WHERE post_id = $1
    RETURNING views;


  @app.post("/view/{post_id}")
  async def view_post(post_id: int):
    await Post.filter(id=post_id).update(views=F("views") + 1)
    post = await Post.filter(id=post_id).get()
    return {"current_views": post.views}