Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db/insert return value #2

Open
p4ulcristian opened this issue Jan 12, 2019 · 2 comments
Open

db/insert return value #2

p4ulcristian opened this issue Jan 12, 2019 · 2 comments

Comments

@p4ulcristian
Copy link

Hi, (lighthouse.core/insert conn {:person/name "Gen Eric"}) returns (1) if it succeded. In the readme at insert I found this: "p is auto-resolved to (get p :person/id)". So what do I misunderstand? how can I get back the id of the inserted row? Thank you, appreciating your work!

@swlkr
Copy link
Member

swlkr commented Jan 15, 2019

Hm, yeah definitely shouldn't have put that in the readme, so I guess it could happen two ways:

  1. I could take it out of the readme and just have it working with postgres and not sqlite
  2. It would work in postgres in the same transaction with returning * and in sqlite, it could work by querying in the insert function and returning the row

Unsure yet about which way to go, I should probably have similar behavior across databases...

@alexjuda
Copy link

alexjuda commented May 3, 2019

Hi, I stumbled upon similar issue, I needed to retrieve id of the inserted row. The workaround I managed to achieve this with was to define my own implementation of transact with an additional argument passed to jdbc/execute! (no ns aliases for clarity):

(defn transact
  "Takes a jdbc connection, a query, an optional map of params interpolated to query, and an options map passed to jdbc
      Ex: (transact conn '[:delete
                           :from todo
                           :where [todo/id 1]])
  "
  ([conn query query-params jdbc-opts]
   (let [schema (lighthouse.core/schema conn)
         db (lighthouse.util/db conn)
         sql (lighthouse.sql/sql-vec db schema query query-params)]
     (clojure.java.jdbc/execute! conn sql jdbc-opts))) ;; notice the additional parameter here
  ([conn query]
   (transact conn query {} {}))
  ([conn query query-params]
   (transact conn query query-params {})))

This way I could pass appropriate option to get the id back in generated keys map:

(let [res (transact conn
                    [:insert :person/name :values ["Alex"]]
                    {}
                    {:return-keys true} ;; <= passed to jdbc/execute!
                    )]
  (get res (keyword "last_insert_rowid()")))

Haven't tested this with Postgres yet, I'm using SQLite at the moment. Do you think it would make sense to add this modification in the lib, @swlkr?
Thanks for awesome library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants