Fork me on GitHub
#sql
<
2020-12-05
>
Aneil Mallavarapu12:12:07

Does anyone know how to inser an enumerated type using honeysql / jdbc-next? I don’t have any problem using jdbc-next

(jdbc/execute-one ["INSERT INTO processes(id, state) VALUES(?, ?);" uuid (jdbc/as-other "ready"))
But when I try something similar with honeysql:
(jdbc/execute-one (-> (insert-into :processes) (values [{:id uuid, :state (jdbc/as-other "ready")}]) sql/format))
I get : java.lang.AssertionError: Assert failed: Alias should have two parts["running"] (= 2 (count x))

Aneil Mallavarapu12:12:18

In case it wasn’t clear, state is an enum type column. E.g.,:

CREATE TYPE STATES AS ENUM ('ready', 'complete', 'error');

CREATE TABLE IF NOT EXISTS processes (
  id UUID DEFAULT uuid_generate_v4(),
  PRIMARY KEY (id),

  state STATES);

dharrigan14:12:46

what does the sql come out as when you do sql/format?

dharrigan14:12:00

It works for me btw

dharrigan14:12:09

txi=> CREATE TYPE STATES AS ENUM ('ready', 'complete', 'error');
CREATE TYPE
txi=> CREATE TABLE IF NOT EXISTS processes (
txi(>   id UUID DEFAULT uuid_generate_v4(),
txi(>   PRIMARY KEY (id),
txi(>   state STATES);
CREATE TABLE
txi=> select * from processes ;
┌──────────────────────────────────────┬───────┐
│                  id                  │ state │
├──────────────────────────────────────┼───────┤
│ b94f550e-f058-41e2-bdb6-e5c09e3cbd38 │ ready │
└──────────────────────────────────────┴───────┘
(1 row)

dharrigan14:12:23

user=> (def s (-> (insert-into :processes) (values [{:id #uuid "b94f550e-f058-41e2-bdb6-e5c09e3cbd38", :state (as-other "ready")}]) sql/format))
#'user/s
user=> (db/execute! s (:txi-db app-config))
{:next.jdbc/update-count 1}

seancorfield17:12:22

@aneil.mallavar update to the latest next.jdbc version. That was fixed a few versions ago.

Aneil Mallavarapu17:12:46

@seancorfield - hmmm. I’m still seeing the issue. These are my dependencies:

[seancorfield/next.jdbc "1.1.613"]
[honeysql "1.0.444"]
@dharrigan When I try your code, I get:
(def s (-> (insert-into :processes) (values [{:id #uuid "b94f550e-f058-41e2-bdb6-e5c09e3cbd38", :state (as-other "ready")}]) sql/format))

Syntax error (AssertionError) compiling at (form-init15174950692797201891.clj:1:8).
Assert failed: Alias should have two parts["ready"]
(= 2 (count x))

Aneil Mallavarapu17:12:26

I’m going to try with a fresh project.

seancorfield18:12:05

If you're getting ["ready"] then you somehow are still using the older next JDBC. That behavior changed in 1.1.610

seancorfield18:12:02

@aneil.mallavar Here's proof of the problem being fixed between 1.1.588 and 1.1.613:

seanc@DESKTOP-30ICA76:~/clojure$ clj -Sdeps '{:deps {honeysql/honeysql {:mvn/version "1.0.444"} seancorfield/next.jdbc {:mvn/version "1.1.613"}}}'
Clojure 1.10.1
user=> (require '[honeysql.core :as sql] '[honeysql.helpers :refer :all] '[next.jdbc.types :refer :all])
WARNING: update already refers to: #'clojure.core/update in namespace: user, being replaced by: #'honeysql.helpers/update
nil
user=> (def s (-> (insert-into :processes) (values [{:id #uuid "b94f550e-f058-41e2-bdb6-e5c09e3cbd38", :state (as-other "ready")}]) sql/format))
#'user/s
user=>
seanc@DESKTOP-30ICA76:~/clojure$ clj -Sdeps '{:deps {honeysql/honeysql {:mvn/version "1.0.444"} seancorfield/next.jdbc {:mvn/version "1.1.588"}}}'
Clojure 1.10.1
user=> (require '[honeysql.core :as sql] '[honeysql.helpers :refer :all] '[next.jdbc.types :refer :all])
WARNING: update already refers to: #'clojure.core/update in namespace: user, being replaced by: #'honeysql.helpers/update
nil
user=> (def s (-> (insert-into :processes) (values [{:id #uuid "b94f550e-f058-41e2-bdb6-e5c09e3cbd38", :state (as-other "ready")}]) sql/format))
Execution error (AssertionError) at honeysql.format/seq->sql (format.cljc:385).
Assert failed: Alias should have two parts["ready"]
(= 2 (count x))
user=>

seancorfield18:12:24

I guess you're using lein so perhaps you need to do lein clean and then restart your REPL?

seancorfield18:12:17

Here's 1.1.610 to show it changed in that release:

seanc@DESKTOP-30ICA76:~/clojure$ clj -Sdeps '{:deps {honeysql/honeysql {:mvn/version "1.0.444"} seancorfield/next.jdbc {:mvn/version "1.1.610"}}}'
Downloading: seancorfield/next.jdbc/1.1.610/next.jdbc-1.1.610.pom from clojars
Downloading: seancorfield/next.jdbc/1.1.610/next.jdbc-1.1.610.jar from clojars
Clojure 1.10.1
user=> (require '[honeysql.core :as sql] '[honeysql.helpers :refer :all] '[next.jdbc.types :refer :all])
WARNING: update already refers to: #'clojure.core/update in namespace: user, being replaced by: #'honeysql.helpers/update
nil
user=> (def s (-> (insert-into :processes) (values [{:id #uuid "b94f550e-f058-41e2-bdb6-e5c09e3cbd38", :state (as-other "ready")}]) sql/format))
#'user/s
user=>

Aneil Mallavarapu18:12:17

Agree, there’s definitely something odd going on in my project. Maybe the project is borked somehow. I’m looking into it.

Aneil Mallavarapu18:12:42

@seancorfield lein clean did the trick.

seancorfield18:12:09

Yeah, that can be a problem with lein, unfortunately. It can, theoretically, happen with the Clojure CLI too but it's rarer in my experience (and easy to "force" a correction with clj -Sforce ... where it recomputes the dependencies and then re-caches them).

Aneil Mallavarapu19:12:10

Thanks again. I’m new to Clojure (though plenty of CL in the distant past). Sorry for using up your time on such remedial issues. I really appreciate the help.

seancorfield20:12:28

No problem at all. That's what we're all here for: to help others in our community!

💯 3
🙏 3