datomic

Ask questions on the official Q&A site at https://ask.datomic.com!
mafcocinco 2020-08-18T13:04:10.204700Z

What is an idiomatic way to express uniqueness within a set of attribute values in Datomic? That is, if I have an attribute that is of type db.type/ref and it is of db.cardinality/many, how do I enforce a uniqueness constraint on the set of values that is being referred to, in the context of the containing value?

favila 2020-08-18T13:09:37.204800Z

Are you saying that the uniqueness constraint is expressed among the entities referenced?

favila 2020-08-18T13:10:16.205400Z

so normal ref uniqueness is not enough, and the referenced entities themselves don’t have inherent uniqueness

favila 2020-08-18T13:10:42.205600Z

would an example be: A person may have many addresses, but only one may be a primary address

favila 2020-08-18T13:11:24.205800Z

or: A person may hold many cards, but they must all have the same color or different numbers

favila 2020-08-18T13:13:37.206100Z

if there’s a fixed number of different states like in the address example, you can break up the single card-many attribute into more attributes with more specific meanings. e.g. instead of :person/addresses, you have :person/address-primary, :person/address-secondaries

favila 2020-08-18T13:15:55.206300Z

if the set is more open (e.g. color or number constraints) but the entities are semantically “components” of the primary entity, you can reverse the direction of reference so the component points to the primary entity, then make a unique composite tuple that includes the component and the other attributes that mark uniqueness

favila 2020-08-18T13:16:55.206500Z

if the referenced entities are not components (i.e. are shared among other entities, so you can’t add an ownership attribute to it), you have to do this through an intermediate entity

favila 2020-08-18T13:17:19.206700Z

finally, you can just use a :db/ensure function that enforces your constraint

mafcocinco 2020-08-18T14:23:09.206900Z

It is more the open set concept, so reversing the relationship might be the way to go, though I’m going to look at :db/ensure first.

mafcocinco 2020-08-18T14:23:17.207100Z

Thank you for your help.

joshkh 2020-08-18T19:12:48.211300Z

is there a path to audit access to a datomic cloud system, perhaps using cloudtrail?

👍 1