This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-08-15
Channels
- # announcements (1)
- # beginners (101)
- # boot (13)
- # cider (38)
- # cljdoc (10)
- # cljs-dev (37)
- # cljsrn (6)
- # clojure (74)
- # clojure-dev (8)
- # clojure-europe (3)
- # clojure-italy (36)
- # clojure-losangeles (2)
- # clojure-nl (5)
- # clojure-spec (15)
- # clojure-uk (49)
- # clojuredesign-podcast (2)
- # clojurescript (52)
- # cursive (6)
- # datomic (19)
- # fulcro (35)
- # graalvm (16)
- # graphql (4)
- # kaocha (1)
- # leiningen (26)
- # luminus (3)
- # re-frame (10)
- # reagent (14)
- # ring-swagger (37)
- # rum (2)
- # schema (4)
- # shadow-cljs (148)
- # spacemacs (13)
- # specter (1)
- # sql (46)
- # tools-deps (3)
- # vim (4)
Hey there, I'm using next-jdbc
and I'm trying to simply insert something and get ID of the thing I inserted (the ID auto increments). However, right now, result
is nil
:
(defn submit-forum-post
"Validate and upload a post to the database"
[post]
(let [body (:body post)]
(when body
(println "Receiving post: " body)
(let [result
(doall (sql/insert! @db-spec :Posts
{ :PostTitle (:post-title body)
:PostContent (:post-content body)
:IsAnonymous (:is-anonymous body)}))]
(println "RESULT: " result)
(json/write-str {:result result})))))
The insert is indeed working as posts are appearing. I'm using this function: https://github.com/seancorfield/next-jdbc/blob/master/doc/friendly-sql-functions.md#insert
and in the source code this function has the comment:
Given a connectable object, a table name, and a data hash map, inserts the
data as a single row in the database and attempts to return a map of generated
keys.
So I'm not quite sure where the problem lies with this. Any ideas? 🙂I was just making sure it was being executed
was unsure when the execution of the insert statement occurred
it does nothing still
am I correct in thinking that this in theory should return the row?
im rather new to sql and jdbc
so I am worried that I'm doing something fundamentally wrong
postgresql
thats a very good question, I don't know if I have that or if I do, how to check
I would expect insert!
to return the entire row inserted into the Post
table. Including any generated key.
yeah, that's my intention as I want to return that ID back to the client so it can create a URL to the post's page
How your determine what version of the postgres driver you are using will depend on what tooling you are using for your project
@hiredman do I need to add that to my deps.edn? I have
postgresql/postgresql {:mvn/version "9.1-901-1.jdbc4"}
ah the one you linked has more uses
let me try that one!
thank you for bringing this to my attention
I use leiningen, so in my project.clj there is a line like this:
[org.postgresql/postgresql "42.2.6"]
okay that is bizarre, connection to my db is unsuccessful
let me work through it
in my dbspec map, I have keys of :dbtype "postgresql", :dbname, :user and :password. If you are using certain connection pooling, you may need :username instead of :user
yeah I had that already and it was working fine with the other package - would I need to use both at the same time or was I right to replace the previous with this one
Replace
That version you were using came from 2011 BTW
oops :'D
well it looks like im on the right track using this one, but clearly there's now something wrong with my way of connecting.
For host
I have localhost:5432
, is this still valid?
Remove that
Just use dbtype dbname user password
okay, that was the key
thank you
I'm surprised that worked before. I guess that old driver allowed weird connection strings
I'm going to need to learn how to re-implement what I had - so before it was localhost:5432
by default but I can pass in the new host to the function I made. In my dockerfile script I have:
cmd ["/usr/local/openjdk-8/bin/java", "-cp", "space.jar", "clojure.main", "-m", "space.api.core", "db:5432"]
which db
routed to wherever docker told it to. This seemed to work before but now it doesn'tEither way though, it worked! next-jdbc
now returns the record which is fantastic
just the plumbing to go
You can specify :host and :port separately. If the host is always localhost you only need :port
If :port is omitted it defaults to 5432 for postgresql
just doing one final build 🙂
I'm putting my phone down and trying to go back to sleep (2:45 am... Jetlag!).
im so sorry!
goodnight!