Datomic: Relationships and refs within a transaction
Not being able to figure this out is what killed all my previous attempts to use Datomic.
You want to transact two Entities that have a relationship between them. You’ve tried all the different variations that Google shows you, but they all give you :db.error/not-an-entity
What you want is the TWO ARGUMENT version of #db/id
1 2 3 4 5 6 7 8 9 |
@(d/transact conn [ {:db/id #db/id[:db.part/user -1] :name "Verna M" } {:db/id #db/id[:db.part/user] :name "Darren Holloway" :mother #db/id[:db.part user -1] }]) |
Here, Verna gets a Temporary ID of -1 in this transaction. We can then reference that same Temporary ID later under :mother
This of course assumes that you have a Schema that defines :mother as a Datomic Reference, such as:
1 2 3 4 5 6 7 8 |
@(d/transact conn [ {:db/ident :parent :db/doc "Reference to Mother" :db/cardinality :db.cardinality/one :db/valueType :db.type/ref :db/id #db/id[:db.part/db] :db.install/_attribute :db.part/db }]) |
Also note that #db/id is simply a Tagged Element (similar to a reader macro), not an inline function or symbol reference or anything else.
Leave a Reply