Fork me on GitHub
#sql
<
2018-02-20
>
Jacob00:02:03

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 work

seancorfield00:02:04

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

seancorfield00:02:58

According to the HugSQL docs, you need to call (hugsql/def-db-fns "db/users.sql") for that to happen.

Jacob00:02:23

oic I was thinking

--name insert-user
would define the query and
(def queries "db/users.sql"...)
would load the sql file? @seancorfield

Jacob00:02:50

ill give that a try thanks @seancorfield

seancorfield00:02:16

(def queries ...) just creates a Var called queries with a docstring of "db/users.sql" and a value of {:connection db-spec}

Jacob00:02:26

do i need to define a connection in

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

seancorfield00:02:30

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.

seancorfield00:02:02

@jisaacs46 The HugSQL docs explain how DB connections are handled.

minikomi02:02:43

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?

shaun-mahood16:02:05

Has anyone got any naming or casing conventions they like when building a new SQL database that translate well to idiomatic Clojure?

seancorfield20:02:19

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

seancorfield20:02:11

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 = ?"

seancorfield20:02:01

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

shaun-mahood20:02:31

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