I learned that in Postgres v9.4 and above, one can do partial updates to jsonb columns.
I am using Peewee on top of my Postgres database. Peewee has a support to declare a column jsonb but I cannot find an efficient way of updating the jsonb column to harness the power of partial update.
Here's what I am doing -
SQL = "update table_name set data = data || %s where tid = %s"
vals = (json.dumps(partial_data), tid)
db_pointer.execute_sql(SQL, vals)
data
is the column name
db_pointer
is the pointer to Postgres database using playhouse.
The above code works fine, but here I am not using Peewee to update it. It just a normal query to the database.
Does anyone know a better way to do this?
As I commented on GitHub, you can instead:
data = {'some': 'data', 'for json field': [1, 2, 3]}
query = ModelClass.update({
ModelClass.data: ModelClass.data.concat(data)
}).where(ModelClass.id == some_id)
query.execute()