Fork me on GitHub
#datomic
<
2019-09-20
>
Brian19:09:31

How do I give a :db/id to an entity? I'm using :db/id when trying to transact my data and I get a "unable to resolve entity". How can I say "if it doesn't exist, make one with this id"?

favila19:09:21

Use a tempid (string) to create a new entity. Note that an entity is inherently it’s id, you don’t “give” it one.

favila19:09:14

have you gone through a tutorial yet?

Brian20:09:45

Tell me if my instructions were incorrect. I have a blacklist entity and I want to add things to a cardinality-many attribute of it. I was told I would be able to use :db/id to "name" it for sake of argument :blacklist and from here on out when I want to add to that blacklist I would be able to include :db/id :blacklist in my map-form transaction. Is that a flawed assumption so far?

favila20:09:08

It’s a simplification? or confusion maybe?

favila20:09:57

What’s really happening is that all data in datomic is assertions and retractions (datoms) which state one “fact”. They are structured like [entity-id, attribute-id, value, transaction, assertion-or-retraction]

favila20:09:48

the map form from a pull is a projection of this, where all assertions sharing the same entity-id are joined together, the entity id itself is put on :db/id, and the attributes+values are map entries

favila20:09:25

similarly, the map form of a transaction is syntactic sugar that expands out into [:db/add entity-id attribute value] assertions

favila20:09:30

added to all this, you don’t have much control over the actual value of the entity id, so it doesn’t make much sense to talk about “naming” an entity

favila20:09:23

if you want a possibly new entity, you use a tempid: [:db/add "some-string-representing-a-tempid" attribute value] or {:db/id "some-string-representing-a-tempid" attribute value}

Brian20:09:02

So is the answer to the "I only want a single blacklist and only want to ever add stuff to that one" to, before any transactions, query for the eid of the blacklist and use that (or a temp-id on first go)?

favila20:09:01

you can use an upserting indexed attribute for this purpose: the index lookup will resolve to an existing entity id or create one if it doesn’t exist yet

favila20:09:17

if this is really a singleton, you can use :db/ident

favila20:09:37

{:db/id "the-blacklist" :db/ident :my/blacklist-entity …}

favila20:09:01

or you can make your own attribute

favila20:09:51

https://docs.datomic.com/on-prem/identity.html discusses various ways of “addressing” an entity

favila20:09:14

(actually I take that back, I’m not sure :db/ident is upserting; but you can at least ensure it is created as part of your schema bootstrapping)

Brian20:09:29

Thank you!