The following SQL statement, towards a multi-schema PostgreSQL (v.12) DB, is a perfectly valid one and it achieves the intended result.
I need to update a deep JSONB value as shown below:
UPDATE schema."Some_table_name" SET jsonb_col = jsonb_set(jsonb_col, '{path,to,key}', '"string value"') WHERE id = 1;
When I try to do the above from python using psycopg2, I don't get any errors, but no update either.
def update_method(schema, path, value):
q = f"""UPDATE {schema}."Some_table_name" SET jsonb_col = jsonb_set(jsonb_col, '{path}', '{value}') WHERE id = 1"""
cur = conn.cursor()
cur.execute(q)
conn.commit()
cur.close()
update_method('schema_name', '{path,to,key}', '"string value"')
I tried to specifically cast the path inside my q statement, as: '{path}'::text[]. Still no error, nor update.
Any idea how I can update a deep JSONB using psycopg2 and a method like the one above?
EDIT - after reading the comments and trying the code of @Abelisto, I've realized what would work (and I feel a bit silly). The following code updates correctly any deep JSONB value:
def update_method(connection, schema, path, value):
q = f"""UPDATE {schema}."Some_table_name" SET jsonb_col = jsonb_set(jsonb_col, '{path}', '{value}') WHERE id = 1"""
cur = connection.cursor()
cur.execute(q)
connection.commit()
cur.close()
update_method(conn, 'schema_name', '{path,to,key}', '"string value"')