Fork me on GitHub
#datalevin
<
2022-09-14
>
dvingo20:09:34

What is the recommended approach for transacting data with cylces? for example:

;; schema
{:user/id              {:db/valueType :db.type/keyword :db/unique :db.unique/identity}
 :user/friends         {:db/valueType :db.type/ref :db/cardinality :db.cardinality/many}
 :user/name            {:db/valueType :db.type/string :db/unique :db.unique/identity}}
(d/transact! conn 
  [{:user/id :user-2 :user/name "user 2" :user/friends [[:user/id :user-2] [:user/id :user-1]]}
   {:user/id :user-1 :user/name "user 1" :user/friends [[:user/id :user-2]]]})
;; =>
Execution error (ExceptionInfo) at datalevin.db/entid-strict (db.cljc:435).
Nothing found for entity id [:user/id :user-1]
Would you just do this with multiple transactions, insert each user without the friends and then submit another transaction to add the friends?

dvingo20:09:53

This only came up during testing when seeding a DB I suppose in practice it's a bit unrealistic as both entities would likely exist already.

Huahai02:09:35

you could use temp ids, like :db/id -1

👍 1
denik15:09:25

have you tried using maps instead of vectors, e.g.

(db/transact! conn
              [{:user/id :user-2 :user/name "user 2" :user/friends [{:user/id :user-2} {:user/id :user-1}]}
               {:user/id :user-1 :user/name "user 1" :user/friends [{:user/id :user-2}]}])