My question is - does Dataomic require the explicit manual creation of unique sequence numbers by the end user? Or is it just the example provided?
I'm reading through the Datomic tutorial.
When I look at the data that gets loaded in seattle-data0.dtm I see on the first two lines:
[
{:district/region :region/e, :db/id #db/id[:db.part/user -1000001], :district/name "East"}
{:db/id #db/id[:db.part/user -1000002], :neighborhood/name "Capitol Hill", :neighborhood/district #db/id[:db.part/user -1000001]}
Notice in particular the values
:db/id #db/id[:db.part/user -1000001],
:db/id #db/id[:db.part/user -1000002]
#db/id[:db.part/user -1000001]
Perhaps you can help me understand - this appears to explicitly require a manually generated unique ID sequence number when preparing data for insert.
Surely in a modern database we can rely on the database to generate sequence numbers for us?
When I go to do my own example schema and data insert - I find that I am required to insert manual ID numbers as well. What am I missing?
When a transaction containing temporary ids is processed, each unique temporary id is mapped to an actual entity id. If a given temporary id is used more than once in a given transaction, all instances are mapped to the same actual entity id. If an entity does not have a unique attribute, then Datomic will assign a new entity id for that entity.
If an entity has a :db.unique/identity attribute, Datomic will upsert. First Datomic will look for an existing entity with the same value for that unique attribute, and use that id. If no such entity exists, Datomic will assign a new entity id.
Datomic provides a number of ways to model identity and uniqueness. All entities have database-unique entity ids. Idents can be used for programmatic names. Unique identities allow transactions to work with domain keys instead of entity ids. Unique values enforce a single holder of an identifying key.
There are some situations where Datomic cannot know that a keyword is an ident. For example, Datomic does not know the semantics of database functions written by you. If you need to convert between idents and entity ids in your own code, you can use the ident and entid methods on the Database class.
To answer your question: No Datomic doesn't require the end user to generate identifiers. What you see in the seattle example are temporary ids.
Every time you want to add some facts about new entities to Datomic, you have to give every new entity a temporary id. This id will be replaced with a real unique id by Datomic.
Now you may ask yourself why do you have to use this temporary ids in the first place? Temporary ids are needed to express relationships between all new entities in one single transaction. In your example, you have the ids:
:db/id #db/id[:db.part/user -1000001],
:db/id #db/id[:db.part/user -1000002]
#db/id[:db.part/user -1000001]
two of them are the same (I'll explain the negative numbers in a moment). That means that the new entity marked with the temporary id #db/id[:db.part/user -1000001]
is the same in both assertions.
Now I have to explain the data literal (other link) #db/id[:db.part/user -1000001]
. #db/id
is the tag for a Datomic temporary id. The tag is followed by a vector of two components :db.part/user
and -1000001
. The first part is the database partition and is mandatory. The second part is optional. If you write just #db/id[:db.part/user]
, you get a fresh (different) temporary id every time this literal occurs. If you write #db/id[:db.part/user -1000001]
you get the same temporary id every time you use the negative index -1000001
. So #db/id[:db.part/user -1000001]
is different to #db/id[:db.part/user -1000002]
.
I don't exactly know why the examples use indices below 1000000. The JavaDoc of tempid where the data literal of #db/id
resolves to, says, that the numbers from -1 (inclusive) to -1000000 (exclusive) are reserved for user-created temp ids. So maybe someone can shed some light on this.
To sum this up: #db/id[...]
are temporary ids to express same entities in one transaction and are replaced by real unique ids by Datomic at the end of the transaction. If you don't have to refer to the same entity in a transaction twice, you are fine with just #db/id[:db.part/user]
for every temporary id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With