Discussion:
Composite keys on entities -- implementation strategies
László Török
2015-02-10 17:30:59 UTC
Permalink
Hi,

there basically two ways (I can think of):

You declare a new attribute with a :db.unique/value constraint. When you
add a new entity to the database, you make sure that you also compute the
value of this attribute and include in the transaction. The transaction
will fail if the uniqueness constraint is violated, and you can retry. (You
can also do a "pre-flight" using datomic.api/with as it is a cheaper way of
finding it out on the peer).

The other way of doing it, is on the transactor. You need to declare a
transaction function [1] that will run inside the transactor. There you can
run your checks and fail if they are violated.

Hope it helps

[1] http://docs.datomic.com/database-functions.html
Can someone shed some light on how to implement composite keys for a
Datomic entity? Any hint/help is much appreciated. I understand Datomic
entity is essentially the ID, and we only store the fact/attribute. But in
my case, the requirement is to make sure a fact exists only along with
couple of other facts, and those facts together point to a thing in my
domain (an Entity and its composite/primary key). I can also have a UUID
attribute for this entity, and use that for my future lookups.
The Datomic document states this - " Datomic does not provide a mechanism
to declare composite uniqueness constraints; however, you can implement
them (or any arbitrary functional constraint) via transaction functions
<http://docs.datomic.com/database-functions.html>."
Again, any help/suggestion is much appreciated. Thanks
-Hari
--
You received this message because you are subscribed to the Google Groups
"Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
László Török
--
Checkout http://www.lollyrewards.com/
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David McNeil
2015-02-11 13:29:22 UTC
Permalink
I used the approach identified here:
https://gist.github.com/wvdlaan/5772825 (it has limitations as noted on the
comments on the gist).

A discussion of the problem and this solution is here:
https://groups.google.com/forum/#!topic/datomic/iQ6CZ5pDMIQ

-David
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Grant Beaty
2015-02-11 16:27:19 UTC
Permalink
I have used a serialization library (Scala's pickle, though any should do)
to serialize all of the keys into one binary or JSON attribute with a
unique identity. This seemed to work fine, though of course you end up with
a redundant attribute.

/Grant
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hari Krishnan
2015-02-11 20:30:43 UTC
Permalink
Thank you for the suggestions. Looks like there are two approaches to
explore. I will try with Clojure's nippy library for serialization. Has
anyone done an upsert with this approach? Also there could be a
possibility for one of the key attribute updates as well.

Transactor function seems to be cleaner. But might have some performance
impact. Let me try it out.

Again, thanks.

-Hari
Post by Grant Beaty
I have used a serialization library (Scala's pickle, though any should do)
to serialize all of the keys into one binary or JSON attribute with a
unique identity. This seemed to work fine, though of course you end up with
a redundant attribute.
/Grant
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...