Fork me on GitHub

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
-- 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 work


I 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? @seancorfield


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}


do i need to define a connection in

(hugsql/def-db-fns "db/users.sql")


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