Fork me on GitHub

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"?


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


have you gone through a tutorial yet?


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?


It’s a simplification? or confusion maybe?


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]


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


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


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


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}


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)?


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


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


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


or you can make your own attribute

favila20:09:51 discusses various ways of “addressing” an entity


(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)


Thank you!