Fork me on GitHub
#honeysql
<
2022-10-26
>
Pragyan Tripathi10:10:43

How can I create honeysql equivalent for:

CREATE TABLE IF NOT EXISTS "Blog" (
    "id" SERIAL NOT NULL,
    "title" TEXT NOT NULL,
    "comments" TEXT[] DEFAULT ARRAY['hello', 'world']::TEXT[],

    CONSTRAINT "blog_pkey" PRIMARY KEY ("id")
);
I am using following:
{:create-table [:blog :if-not-exists],
 :with-columns
 [[:id :uuid [:not nil]]
  [:comments
   [:raw "text[]"]
   [:default [:raw "ARRAY[hello, world]::text[]"]]]
  [[:constraint :blog_pkey] [:primary-key :id]]]}
But this results in
["CREATE TABLE IF NOT EXISTS blog (id UUID NOT NULL, comments TEXT[] DEFAULT ARRAY[HELLO, WORLD]::TEXT[], CONSTRAINT blog_pkey PRIMARY KEY(ID))"]
Cases in the default values are incorrect. Is there a way I can do it without using :raw operator.

seancorfield19:10:41

There's a GH issue open that mentions this https://github.com/seancorfield/honeysql/issues/386 -- DDL support is very tricky to get right since it is so vendor-specific and the heuristic for upper-casing parts of the declaration is too zealous at the moment. So the TL;DR: is "No, you can't do this right now" but I plan to fix it at some point.

Pragyan Tripathi03:10:33

Got it. Thanks. Are there ideas about how you are planning to solve it? I can try to implement it for my use case.

seancorfield04:10:30

I don't know yet. DDL is horribly inconsistent and doesn't follow the "normal" rules of SQL syntax.

👍 1
seancorfield04:10:48

There are good reasons why HoneySQL v1 pretty much didn't touch DDL at all 🙂

ts150313:10:03

Hey @U02JRAM6CBA I ended up with custom clause like this one

ts150313:10:24

last screenshot didn’t capture all code

💯 1
Pragyan Tripathi00:10:17

This is great. WIll give adding custom-clause a try.