Fork me on GitHub
#sql
<
2021-07-31
>
markbastian17:07:25

Is there a straightforward way to do the following in one shot:

(let [{::jdbc/keys [datasource]} (system/system) ;A jdbc datasource is in the map
        {:keys [address_id] :as person} {:first_name "Sherlock"
                                         :last_name  "Holmes"
                                         :address_id {:street "Baker"}}
        {:keys [ADDRESSES/ID]} (sql/insert! datasource "ADDRESSES" address_id)]
    (sql/insert! datasource "PERSONS" (assoc person :address_id ID)))
The PERSONS table has an ADDRESS_ID column that is a FK to the ID column in the ADDRESSES table.

emccue17:07:58

you can use a transaction

emccue17:07:06

that does it in "one shot"

emccue17:07:04

(let [{::jdbc/keys [datasource]} (system/system)
  (jdbc/with-transaction [t datasource]  ... rest of your code, but use t instead ...))      

emccue17:07:11

depending on how you define one shot

markbastian17:07:27

Perfect, that is exactly what I was looking for. Thanks!