Fork me on GitHub
#honeysql
<
2021-04-01
>
aratare06:04:48

Hi there. I’m having a problem with Postgresql not being very happy with HoneySQL@v2 converting :foo/bar to "foo"."bar" when set-ing a new value. For example:

(-> (helpers/update :user)
      (helpers/set {:foo/bar 1})
      (helpers/where [:= :user/id id])
      (helpers/returning :*)
      (sql/format {:dialect :ansi}))
will not work because PSQL requires that columns do not contain the table name. Is there a way to disable this behaviour? I can just convert those keywords to simple keywords but that’s a bit ugly. Thanks in advance.

seancorfield06:04:22

Open an issue on github and I'll fix it tomorrow. It's a bug.

aratare06:04:34

Will do. Thanks 🙂

aratare13:04:46

Hi there. Where should I be looking into if I want to configure automatic conversion between kebab-case and snake_case for both table names and column names? Thanks in advance.

dharrigan13:04:27

it does that already

aratare13:04:12

It doesn’t for me for some reason…

aratare13:04:05

I’m using Hikari for pooling, and every time I use honeysql it always yell at me for not having the proper relation

dharrigan13:04:06

are you using next jdbc?

dharrigan13:04:16

do you have csk on your classpath?

aratare13:04:31

I dont know what that is sorry

dharrigan13:04:39

camel snake kebab

aratare13:04:43

oh yeah I do

aratare13:04:49

I have it in lein

aratare13:04:06

do I need to also require it like next.jdbc.datetime?

aratare13:04:39

so here’s what I have atm:

;; conn
(connection/->pool HikariDataSource (select-keys env [:jdbcUrl]))

(s/defn read-user-tab :- [TabDB]
  [{:user/keys [id]} :- {:user/id s/Uuid}]
  (log/info "Reading tabs for user id" id)
  (nj/with-transaction [tx db]
    (nj/execute! tx (-> (helpers/select :*)
                        (helpers/from :tab)
                        (helpers/where [:= :tab/user-id id])
                        (sql/format {:dialect :ansi})))))

aratare13:04:19

I’m getting yelled at that user-id relation doesn’t exist

aratare13:04:29

it’s called user_id in the db

dharrigan13:04:31

you don't need the :tab/user-id

dharrigan13:04:41

just [:= :user-id id]

aratare13:04:23

let me test real quick

aratare13:04:26

@dharrigan No luck. It’s still giving me ERROR: column "user-id" of relation "tab" does not exist

aratare13:04:29

with or without the tab namespace

aratare13:04:39

changing it to user_id works as expected

dharrigan13:04:55

question why dialect ansi?

aratare13:04:14

because I want quoting

dharrigan13:04:26

the quoting may disable the undasherizing

aratare13:04:23

you’re right

aratare13:04:29

I just removed it and it works

aratare13:04:49

haha is that even intended?

dharrigan13:04:07

probably. Sean wouldn't do anything without a reason 🙂

dharrigan13:04:27

it's probably because as you're using ansi, it won't make any assumptions about the identifiers

dharrigan13:04:33

it'll treat them "literally"

dharrigan13:04:43

rather than trying to undasherize them

aratare13:04:06

I mean I can rename the table, but it’s hard trying to find anything more suitable than user

aratare13:04:37

I have another problem it seems. The returned map still keeps it as user_id

aratare13:04:38

Well that’s tomorrow’s problem. Time for me to do something else 😅

dharrigan14:04:06

User is the only convention I break (keeping tables singular) and I call it users

dharrigan14:04:16

makes life simple 🙂

seancorfield16:04:06

@rextruong To clarify some stuff about the camel snake kebab stuff: that only affects next.jdbc and it only affects 1) names in ResultSet’s that the library converts to Clojure and 2) names in Clojure data structures that the library converts to SQL — it does not (cannot) affect anything in the SQL string itself.

seancorfield16:04:14

HoneySQL V2 defaults to :ansi dialect so you don’t need to provide it as an option (it’s relatively harmless to do so). If you specify a dialect, you get quoting. You can also ask for :quoted true to get quoted with the current/default dialect.

seancorfield16:04:49

If you ask HoneySQL to quote names, it does so without dealing with -/`_` — in both V1 (which is the code @dharrigan linked to) and V2 here: https://github.com/seancorfield/honeysql/blob/v2/src/honey/sql.cljc#L150-L169

seancorfield16:04:20

user=> (-> {:select :* :from :tab :where [:= :tab/user-id 42]} (sql/format))
["SELECT * FROM tab WHERE tab.user_id = ?" 42]
user=> (-> {:select :* :from :tab :where [:= :tab/user-id 42]} (sql/format {:quoted true}))
["SELECT * FROM \"tab\" WHERE \"tab\".\"user-id\" = ?" 42]

seancorfield16:04:45

See some of the important differences around V1/V2 handling of names here https://github.com/seancorfield/honeysql/blob/v2/doc/differences-from-1-x.md#option-changes