sql

Shlomit Sibony 2023-06-25T14:26:55.327339Z

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!

Shlomit Sibony 2023-06-25T14:29:01.498209Z

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]}])

Shlomit Sibony 2023-06-25T14:31:57.799789Z

this is the cases param:

[{:condition {:operands [{:values ["a" "b" "c"], :dimension "ms", :operator "is"}], :operator "or"}, :targetValue "value 1"}]

seancorfield 2023-06-25T16:43:36.221849Z

https://clojurians.slack.com/archives/C03S1KBA2/p1687673308250129?thread_ts=1687673308.250129&cid=C03S1KBA2 - but see the caveats in the reply thread.

Shlomit Sibony 2023-06-25T21:19:13.767469Z

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? ๐Ÿ™

seancorfield 2023-06-25T21:26:44.057359Z

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?

sandqvist 2023-06-26T07:48:32.316449Z

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.

Shlomit Sibony 2023-06-26T09:42:07.723599Z

I already solved it, this way ๐Ÿ™‚ https://clojurians.slack.com/archives/C66EM8D5H/p1687764675797119?thread_ts=1687728534.499839&cid=C66EM8D5H

seancorfield 2023-06-25T23:02:53.883339Z

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.