Hi ๐
Iโm pretty new to honeysql + next.jdbc and trying to insert a jsonb[] to a postgres table, https://github.com/seancorfield/next-jdbc/blob/8e68c1be61c9aa7d71d0faa373accbe2762e35c8/doc/tips-and-tricks.md#working-with-json-and-jsonb worked for select statements without issues, but when trying to insert-into I get (a pretty expected) error column "cases" is of type jsonb[] but expression is of type jsonb
1. How can I extend the SettableParameter protocol to support jsonb[] insertions?
2. Select worked seamlessly as I used jsonb_agg function, is there a similar workaround for insert? (On the query level)
thanks!
this is the query:
(-> (insert-into :cd)
(values [{:name name
:description description
:account_id account-id
:created_at (sql-now)
:updated_at (sql-now)
:created_by uuid
:updated_by uuid
:cases [:lift cases]}])this is the cases param:
[{:condition {:operands [{:values ["a" "b" "c"], :dimension "ms", :operator "is"}], :operator "or"}, :targetValue "value 1"}]https://clojurians.slack.com/archives/C03S1KBA2/p1687673308250129?thread_ts=1687673308.250129&cid=C03S1KBA2 - but see the caveats in the reply thread.
looking only on the settableParameter workaround, it does solve the type issue but Iโm still getting syntax error on the query, this is the query (I guess the settableParameter should have converted the map vector to a json vector, but it havenโt):
INSERT INTO cd
(description, account_id, name, updated_at, id, updated_by, cases, created_by, created_at) VALUES ('ss', 'acc-123', 'shlomit', 2023-06-26 00:02:24.677, '02a1a1a0-f426-4b83-b1af-a6c0f29fe474', '', [{:condition {:operands [{:values ["a" "b" "c"], :dimension "ms", :operator "is"}], :operator "or"}, :targetValue "value 1"}], '', 2023-06-26 00:02:24.677)
any idea? ๐ค
another possible solution:
the query I executed manually and managed to insert a jsonb[]:
insert into cd (...,cases, ...) VALUES (..., ARRAY['{
"targetValue": "A",
"condition": {
"operator": "or",
"operands": [
{
"operator": "is",
"dimension": "channel",
"values": [
"a"
]
}
]
}
}','{
"targetValue": "B",
"condition": {
"operator": "or",
"operands": [
{
"operator": "is",
"dimension": "channel",
"values": [
"b"
]
}
]
}
}']::jsonb[], ...);
but I have no idea how to model it with honeysql given the clojure map vector, do you know a simple way to do it? ๐I don't use PG so I don't deal with JSON in the database. You could ask in #honeysql and see what PG users say there?
I am on my phone so cannot paste a working solution, but you can make your own type with defrecord and extend SettableParameter for it. Then you can wrap the data in an instance of your type and unwrap it in the protocol implementation. HoneySQL has a "value" wrapper which you may need to use as the outermost wrapper to prevent HoneySQL from trying to format your type.
I already solved it, this way ๐ https://clojurians.slack.com/archives/C66EM8D5H/p1687764675797119?thread_ts=1687728534.499839&cid=C66EM8D5H
This is a very minor release indeed but the last release was over two months ago so this felt like a good bit of housekeeping.