Fork me on GitHub
#sql
<
2018-06-18
>
bwstearns16:06:20

So I'm running into an issue using YesQL and transactions. The overall issue is that I can't get the id of a newly created record back from within the transaction. Is this just a limitation of using transactions? I tried using RETURNING in the sql but the db called it a syntax error 😕

; This prints `nil` even though the client is created in the db
(defn create-client [client-info]
    (jdbc/with-db-transaction [tx db-spec]
      (let [client (-> client-info
                     hash-password
                     create-client<! {:connection tx})]
      (println client)
      client)))

; This creates a client row and prints it to the output
(defn create-client [client-info]
    (let [client (-> client-info
                     hash-password
                     create-client<!)]
    (println client)))

bwstearns16:06:23

It seems like create-client<! isn't returning the row like it does outside of a transaction.

valtteri16:06:08

It should work similarly within the transaction

bwstearns16:06:11

reading through the yesql generate.clj file now to figure out what might be going on

valtteri16:06:42

You might need parenthesis around create-client<! {:connection tx} in your threading macro?

bwstearns16:06:24

hm. I'll give that a try.

bwstearns16:06:08

that worked! thanks so much

bwstearns16:06:21

Any idea how it was still managing to insert?

bwstearns16:06:38

I thought a mistake like that would have thrown an error

dadair16:06:48

do you have yesql set up so that queries automatically get the db-spec?

dadair16:06:04

it could be using the pre-defined one instead of the override from the {:connection tx}

bwstearns16:06:26

oh that would make sense.

dadair16:06:10

basically the create-client<! was being called without the override connection map (due to missing parens), so that’d be my guess

valtteri16:06:30

Yep, that’s the thing here.

bwstearns16:06:43

You guys are great. It would have taken me quite a bit of time to notice that.

valtteri16:06:58

Hint: you can macroexpand threading macros to see if it turns out to something that makes sense

valtteri16:06:22

Like here you would’ve probably noticed that the transaction example would’ve had {:connection tx} calling the result of create-client<! 🙂

valtteri16:06:08

I tend to evaluate the code in as small chunks as possible. Clojure makes it fun and easy compared to almost any other language. 👍

bwstearns16:06:31

oh. good call.