Skip to content

Instantly share code, notes, and snippets.

@anti1869
Created January 6, 2017 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anti1869/9c68342beef4fdfd57afbec7eb0da200 to your computer and use it in GitHub Desktop.
Save anti1869/9c68342beef4fdfd57afbec7eb0da200 to your computer and use it in GitHub Desktop.
Increment jsonb field by 1 + value in another column using Peewee
# Suppose table like this
#
# item
# ----
# like_count INTEGER
# snippet JSONB
#
# You want 'like_count' key in snippet also
# and now there is a time to increment
await Item.db_manager.execute(
Item
.update(
like_count=Item.like_count + 1,
snippet=SQL("jsonb_set(snippet,'{\"like_count\"}', to_jsonb(item.like_count + 1))")
)
.where(Item.id == requested_id)
)
# It is stripped nonsense example, but this is how you can build composite fields in jsonb in one update query
# along with usual fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment