Korma Sql model not accepting url var

148 views
Skip to first unread message

The Dude (Abides)

unread,
Feb 11, 2014, 3:22:54 AM2/11/14
to clo...@googlegroups.com
Hi, I'm getting an error calling a record from an id passed via a url using Korma Sql. The error says:

org.postgresql.util.PSQLException

ERROR: operator does not exist: smallint = character varying Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 57

I have a list of members, with a url /member/:id to call profile for that member in the view showing the member list. Here's my 3 moving parts:

ROUTE

(GET "/member/:id" [id] (get-the-member id))

FUNCTION

(defn get-the-member [id]
    (layout/render
      "member/profile.html"
      {:member (db/get-member-url id)}))

MODEL

(defn get-member-url [id]
  (first (select members
                 (where {:id id})
                 (limit 1))))

Now if I hard code the id number in the model, it works, but its not accepting the id var as an integer. How would I give it an explicit typecast in this instance. Or would it perhaps be better to use java.jdbc or another ORM like Sql Lingov, HoneySQL, Clojureql or clojure-sql? Rest of crud working fine, but id var not being accepted by the model. The model itself works if an id number is hardcoded. Perhaps I'm missing some simple syntax point here?

Shantanu Kumar

unread,
Feb 11, 2014, 4:16:43 AM2/11/14
to clo...@googlegroups.com
Can you post the SQL template (with ? symbols) you are trying to execute? Usually, with PostgreSQL you can specify ?::integer in place of ? to imply that the passed parameter is an integer.

Shantanu

Michael Klishin

unread,
Feb 11, 2014, 4:21:13 AM2/11/14
to clo...@googlegroups.com

2014-02-11 12:22 GMT+04:00 The Dude (Abides) <exe...@gmail.com>:
(GET "/member/:id" [id] (get-the-member id))

id here likely ends up being a string but your Postgres column
is an integer. Try (Integer/valueOf id).

--
MK

http://github.com/michaelklishin
http://twitter.com/michaelklishin

exe...@gmail.com

unread,
Feb 11, 2014, 9:40:39 PM2/11/14
to clo...@googlegroups.com
Hi Shantanu,

I finally got it working as follows:

(defmulti parse-int type)
(defmethod parse-int java.lang.Integer [n] n)
(defmethod parse-int java.lang.String [s] (Integer/parseInt s))

(defn get-a-member [id]
  (layout/render "member/profile2.html"
    {:member (db/get-member-url (parse-int id))}
    (session/put! :member-id (parse-int id))))

So parse-int ensures id is specified as an integer for postgres.

I have another question. I'd like to get the username from this result set and put it in session. How would I add that to the above code. If I add :username username to the session PUT statement, it doesn't recognize it.

(defn get-a-member [id]
  (layout/render "member/profile2.html"
    {:member (db/get-member-url (parse-int id))}
    (session/put! :member-id (parse-int id) :username username)))

So I look up the record, and from that record I want to extract id and username and put them in session as member-id and username.

Btw, would you recommend using an ORM or straight java.jdbc and if an ORM, any particular one you'd recommend?

Best,
Pardeep.


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/F9S5W9xEhWs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Shantanu Kumar

unread,
Feb 12, 2014, 12:38:27 AM2/12/14
to clo...@googlegroups.com

Btw, would you recommend using an ORM or straight java.jdbc and if an ORM, any particular one you'd recommend?

ORMs are not prevalent in Clojure. You can use java.jdbc (the 0.3.x series or later) with one of the SQL generators (HoneySQL, YeSQL etc) that you like. See what works for you and post your feedback on this forum.

Shantanu

exe...@gmail.com

unread,
Feb 12, 2014, 2:19:09 AM2/12/14
to clo...@googlegroups.com
Hi Shantanu,

Am trying out the diff solutions and will select one and post my results in a couple days. Will likely just use java.jdbc

Best,
Pardeep.

Reply all
Reply to author
Forward
0 new messages