Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datomic - requires explicit manual coding of unique IDs?

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?

like image 221
hawkeye Avatar asked Jan 28 '13 10:01

hawkeye


People also ask

How does Datomic handle temporary IDs in a transaction?

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.

How does Datomic assign an entity ID?

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.

How do I model identity and uniqueness in datdatomic?

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.

Why can't Datomic tell if a keyword is an ident?

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.


1 Answers

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.

like image 99
Alexander Kiel Avatar answered Oct 17 '22 02:10

Alexander Kiel