sql

All things SQL and JDBC...
Aneil Mallavarapu 2020-12-05T12:27:07.412800Z

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 Mallavarapu 2020-12-05T12:35:18.414500Z

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);

dharrigan 2020-12-05T14:03:46.415700Z

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

dharrigan 2020-12-05T14:28:00.416200Z

It works for me btw

dharrigan 2020-12-05T14:28:09.416400Z

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)

dharrigan 2020-12-05T14:28:23.416600Z

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}

seancorfield 2020-12-05T17:05:22.418Z

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

Aneil Mallavarapu 2020-12-05T17:07:45.418300Z

ooof. sorry.

Aneil Mallavarapu 2020-12-05T17:07:49.418500Z

& thanks.

Aneil Mallavarapu 2020-12-05T17:20:46.420500Z

@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 Mallavarapu 2020-12-05T17:21:26.421Z

I’m going to try with a fresh project.

seancorfield 2020-12-05T18:09:05.423Z

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

seancorfield 2020-12-05T18:20:02.423700Z

@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=>

seancorfield 2020-12-05T18:20:24.424400Z

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

seancorfield 2020-12-05T18:21:17.425100Z

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 Mallavarapu 2020-12-05T18:22:17.425500Z

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

Aneil Mallavarapu 2020-12-05T18:29:42.426Z

@seancorfield lein clean did the trick.

Aneil Mallavarapu 2020-12-05T18:29:50.426200Z

🙏

seancorfield 2020-12-05T18:35:09.427900Z

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 Mallavarapu 2020-12-05T19:24:10.429300Z

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.

seancorfield 2020-12-05T20:44:28.429800Z

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

💯 1
🙏 1