Created
January 6, 2017 20:28
-
-
Save anti1869/9c68342beef4fdfd57afbec7eb0da200 to your computer and use it in GitHub Desktop.
Increment jsonb field by 1 + value in another column using Peewee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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