Fork me on GitHub
#honeysql
<
2022-09-19
>
ts150308:09:28

Hello guys! Is it possible to use lower case table names when constructing create-table query? For example I have a table in my Postgres - "user" quoted, so in all my queries I should use a lover case name And I’m trying to do something like this

(sql/format {:create-table :table
             :with-columns [[:user :uuid [:references :user]]]}
            {:quoted true})
which gives me ["CREATE TABLE \"table\" (\"user\" \"UUID\" REFERENCES \"USER\")"] string with upper case "USER" as a table name. But Postgres doesn’t recognize it as an existing table user. and I see that HoneySQL is calling upper-case function for all columns here.

seancorfield22:09:06

There's no real good solution right now but it's something I'm looking at because the logic around DDL creation and casing is... a bit of a hack. If you're on a recent version of HoneySQL, you could try [:references :'user] which should treat user as something literal but will probably not quote the name.

seancorfield22:09:24

As I suspected, using the "literal name" escape hatch also (deliberately) avoids quoting:

user=> (sql/format {:create-table :table
             :with-columns [[:user :uuid [:references :'user]]]}
            {:quoted true})
["CREATE TABLE \"table\" (\"user\" \"UUID\" REFERENCES user)"]
user=>

ts150310:09:39

Thanks, but I think Postgres wouldn’t accept that as user is a reserved keyword and have to be quoted

ts150310:09:25

I ended up with some custom clause for now

(defn format-raw-update [_ x]
  [(str "UPDATE " (first (sql/format-expr [:inline x])))])


(sql/register-clause! :update-raw format-raw-update :update)

ts150310:09:49

seems a little bit hacky but it works. Will wait for a proper solution. :thumbsup:

ts150308:09:59

Is there a way to tell HoneySQL to leave the column name in lower case?