honeysql 2025-03-25

it seems like honey sql replaces forward slash with . eg. :my-field/id becomes my_field.id is there a way to keep the slash? it seems postgres supports slashes in field names? for that matter how can i keep dashes as well?

oh, i think i might have figured it out, it is some combination of a keyword of a quote (not sure if i’m saying that right)

Yeah, :'my-field/id. But note that it will not replace - with _, so you have to use :'my_field/id.

(defmacro kw->quoted-kw [kw]
    (let [as-txt (symbol (str "'" (namespace kw) "/" (name kw)))]
      `(keyword (quote ~as-txt))))
I built this to convert a keyword but it won’t quote it, although it does leave the characters i want.

I can get it to work with a literal :'my-field/id but having trouble building a general purpose kw->quoted-kw

symbol parses the string so it ends up being a symbol with a namespace. keyword inherits the namespace, so it's exactly the same as if you wrote :'.../... by hand anyway.

since i always need to format keywords this way should i be looking at registering a quote-fn somehow or maybe a default-quote atom or something similar?

What database are you working with, that you have / in column names?

i’m migrating a mongo db into postgres and would prefer to keep the same naming conventions unless i bump into solid reasons to change that.

You might get close to what you want with:

(honey.sql/register-dialect!
  :mongo
  {:quote    #(honey.sql/strop "\"" % "\"")
   :col-fn   #(if (keyword? %) (subs (str %) 1) (str %))
   :parts-fn vector})
and then
(honey.sql/set-dialect! :mongo)

I was barking up that tree, i’ll see if this version helps get me across the line 🙏

The existing :nrql dialect treats :my-field/id as a single column name, but uses MySQL-style quotes. This is a copy of that but using ANSI-style quotes.

thanks, this worked great