This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-02-20
Channels
- # architecture (25)
- # beginners (68)
- # cider (10)
- # clara (3)
- # cljs-dev (90)
- # cljsrn (16)
- # clojure (132)
- # clojure-austin (7)
- # clojure-berlin (3)
- # clojure-czech (1)
- # clojure-dusseldorf (1)
- # clojure-greece (5)
- # clojure-italy (39)
- # clojure-spec (5)
- # clojure-uk (78)
- # clojured (2)
- # clojurescript (92)
- # community-development (6)
- # cursive (7)
- # data-science (1)
- # datascript (14)
- # datomic (32)
- # duct (8)
- # emacs (5)
- # figwheel (3)
- # fulcro (47)
- # hoplon (12)
- # jobs (10)
- # luminus (16)
- # lumo (5)
- # off-topic (1)
- # onyx (2)
- # parinfer (47)
- # pedestal (6)
- # re-frame (10)
- # reagent (2)
- # reitit (61)
- # ring (8)
- # ring-swagger (16)
- # shadow-cljs (116)
- # sql (17)
- # utah-clojurians (2)
- # vim (1)
Hey guys im trying to insert a row into my database using repl with hugsql and i keep getting this error CompilerException java.lang.RuntimeException: Unable to resolve symbol: insert-user in this context, compiling:(/private/var/folders/gp/jc7b8tq15rqc575ylrzjtqkc0000gn/T/form-init1392785922584863167.clj:1:1)
user_model.clj
(ns hipst.db.user-model
(:require [hugsql.core :as hugsql]))
(def db-spec {:dbtype "postgresql" ;#1
:classname "org.postgresql.Driver"
:dbname "postgresql"
:host "localhost"
:user "hipstr"
:password "hipstr"})
(def queries "db/users.sql"
{:connection db-spec}) ; #2
users.sql
-- name: insert-user :<! :n
-- Inserts a new user into the Users table
-- Expects :username :email, and :password
INSERT INTO users(username, email, password)
VALUES (:username, :email, :password)
I tried loading the `user_model.clj in the repl with
(load "db/user_model")
and everything appeared to work just fine but when I run
(insert-user {:username "Jacobi" :email "" :password "12345"})
I get the above error. I have also tried running
(insert-user db-spec {:username "Jacobi" :email "" :password "12345"})
and i get the same error.
Not sure why the insert doesn't workI don't know much about HugSQL but I don't see anywhere in your code where you're asking it to load your SQL file and define your queries @jisaacs46
According to the HugSQL docs, you need to call (hugsql/def-db-fns "db/users.sql")
for that to happen.
oic I was thinking
--name insert-user
would define the query
and (def queries "db/users.sql"...)
would load the sql file?
@seancorfieldNope.
ill give that a try thanks @seancorfield
(def queries ...)
just creates a Var called queries
with a docstring of "db/users.sql"
and a value of {:connection db-spec}
Also, instead of using load
, you should probably use require
to load namespaces (since that's what Clojure itself will do). So (require '[hipst.db.user-model :as um])
for example. And then you can access the Vars from that namespace via the um/
alias.
@jisaacs46 The HugSQL docs explain how DB connections are handled.
When using HugSQL, how are you guys aggregating rows in many-to-many/one-to-many results? Using SQL, or reducing the rows in clojure?
Has anyone got any naming or casing conventions they like when building a new SQL database that translate well to idiomatic Clojure?
@shaun-mahood We've adopted lower_snake_case
for SQL entity names (tables, columns) which is a reasonably close match to Clojure's lower-kebab-case
... but we just use the default clojure.java.jdbc
:identifiers
/ :entities
so we do not map -
/`_` in either direction.
That means that we have maps with :names_like_this
but at least our SQL matches "SELECT some_col FROM my_table WHERE the_id = ?"
We considered mapping the `-`/`_` but then you have a mix of `:name-like-this` and `names_like_this` depending on how you're building your SQL and/or interacting with the DB...
@seancorfield: That's kind of the direction I've been leaning - it sure feels strange though after so many years of MS SQL and all the different vendor DBs with mixed case names. I kind of want to use SCREAMING_SNAKE_CASE
just for the name but that's probably not ideal :)