Question on how to use jsonb_set with postgres
Hi!
Let's assume this setup:
create table entities {
id SERIAL PRIMARY KEY,
props JSONB
};
INSERT INTO entities (props) VALUES('{"attr": "value-1"}');
Now I would like to use this query:
-- :name update-attr! :! :n
UPDATE entities
SET props = jsonb_set(props, '{"attr"}', :val)
WHERE id=:id
In clojure I do this:
(update-attr! {:id 1 :val "value-2"})
But there are several issues with this, and I do not know how to solve them:
- I would have to change the clojure part to add double quotes like so
:val "\"value-2\"". This will make the resulting sql statement look right, but psql still complaints that it can not execute the statement (copy+paste the statement into the psql console will work though). I managed to find a way to execute it with hugsql, but this doesn't seem right to me, since I am no longer using your sql injenction prevention. Check out how I did it: - Change the sql file like so:
jsonb_set(props, '{"attr"}', :sql:val). Change the clojure part:(update-attr! {:id 1 :val "'\"value-2\"'"}). This code will now update the row succesfully.
I would be happy if I could use your sql injenction prevention methods, but I couldn't find a way.
Do you have an idea what I am missing?
Kind regards,
René
@create-renegr Hi! In order to take advantage of the sql-injection protecting parameter-binding features of HugSQL's value parameters, you will need to take a look at extending the underlying clojure.java.jdbc protocols to support a Clojure data type (either a built-in type or a type/record of your own) to be treated as a jsonb object.
Have a look at the following blog post for examples of extending these protocols to support postgresql's json data type. The jsonb would likely be similar.
Hi,
thanks for your quick response.
The postgres error message I receive when using just :val instead of :sql:val is this:
ERROR: function jsonb_set(jsonb, unknown, character varying) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Position: 65
Does this mean, that I need to make sure, that instead of "charactery varying" I get something like String?
I don't understand why I need to make a protocol to support Clojure Strings. Because the :val is just a plain String, which is already supported by HugSQL.
I can't see (yet), why your proposed solution would solve my actual problem.
Kind regards,
René
@create-renegr Hey, sorry for the slow reply on this. I missed your response the other day.
You are right: for jsonb_set usage you do not need to implement the protocols I suggested as long as you do the following:
- Provide explicit type casts to Postgresql for your json fields (this is hinted at in the error you are receiving).
- Pass in valid json (for json string, it needs the double quote) when calling your Clojure function.
Here's an example:
(deftest q52
(testing "pg jsonb_set"
(let [db (:postgresql dbs)]
(hugsql/db-run db "drop table if exists t1" {} :!)
(hugsql/db-run db "create table t1 (id serial primary key, props jsonb)" {} :!)
(hugsql/db-run db "insert into t1 (props) values ('{\"attr\": \"value-1\"}')" {} :!)
(hugsql/db-run db
"update t1 set props = jsonb_set(props, '{\"attr\"}', :val::jsonb) where id = :id"
{:id 1 :val "\"value-2\""} :!)
(hugsql/db-run db "drop table t1" {} :!))))
That did the job!!
Thank you very much for the explanation. Now that makes sense. Wasn't sure which layer had to provide the explicit typecast.
Closing the question, since you provided a clear answer.