Skip to content

Instantly share code, notes, and snippets.

@dadair-ca
Created September 27, 2016 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadair-ca/78be2fa2e0dd880c6ebd37319d12f47d to your computer and use it in GitHub Desktop.
Save dadair-ca/78be2fa2e0dd880c6ebd37319d12f47d to your computer and use it in GitHub Desktop.
Clojure postgresql enum type conversion

Given an enum type e_role, being represented in clojure-land as a string, use the enum in a query and typecast inside it.

Note the ?::e_role:

INSERT INTO accounts (account_id, email, password, account_role)
       VALUES (DEFAULT, :email, :password, ?::e_role);

Use positional arguments for the enum(s) (note the :? [..]):

;; using YESQL (https://github.com/krisajenkins/yesql)
(defquery create-account! "api/queries/create_account.sql")

(create-account! {:? [role]
                  :email email
                  :password password}
                 {:connection (:spec db)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment