Clojure java.jdbc return-keys option only returns one row

Viewed 444

Using org.clojure/java.jdbc "0.7.11" and PostgreSQL 11.5

It seems the return-keys option only returns the first row as a map, and not the complete list

Example:

(jdbc/execute!
  db
  "INSERT INTO test_table(letter) VALUES ('A'), ('B');"
  {:return-keys ["letter"]})

=> {:letter "A"}

What I was hoping to get returned would be something like ({:letter "A"} {:letter "B"})

Both rows are inserted into the database with no issues.

I am aware I could do this using an insert-multi instead and get the rows back, but what I really want to do here is an update, so it needs to be an execute, just thought this was the simplest way to show the issue.

Does anyone know why I'm only getting one row of keys returned, and is there a way to return all rows?

1 Answers

I think what you will probably need is:

(jdbc/execute!
  db
  ["INSERT INTO test_table(letter) VALUES (?)"
   ["A"] ["B"]]
  {:return-keys ["letter"] :multi? true})

Specifying :multi? true causes the library to use .executeBatch instead of .executeUpdate and you should get multiple results back.

If you use next.jdbc instead, you won't have this problem:

(next.jdbc/execute!
  db
  ["INSERT INTO test_table(letter) VALUES ('A'), ('B')"]
  {:return-keys ["letter"]})

This should return both inserted keys.

Note that clojure.java.jdbc is considered "Stable" which means it is no longer getting new functionality. All current and future development work is going into next.jdbc at this point (partly because of issues like the above). next.jdbc is the 1.0 release that clojure.java.jdbc never had.

https://github.com/seancorfield/next-jdbc

Related