Dave Bryand
Dave Bryand

Reputation: 621

Properties on Datomic ref relationships

I'm trying to model a schema where a list can have many items and each item can belong to many lists. It's clear to me that I can have a :list/items ref type to model the relationship, but I'd like to also have a rank attribute which determines an item's position in each list where it exists. How might one do such a thing?

Upvotes: 2

Views: 357

Answers (2)

mwal
mwal

Reputation: 3113

Heterogenous tuples, added in June 2019, are a new modelling option here.

An attribute value, i.e. the v in the eavto 5-tuple, can now itself be a tuple. This is a clojure vector of max length 8.

Official blog post announcement.

Discussion of the release on twitter.

Note the example in the docs above uses

:db/tupleTypes [:db.type/long :db.type/long]

which is a little strange as the point is heterogenous tuples, so in the case of the OP this would be:

{:db/ident       :list/item
 :db/valueType   :db.type/tuple
 :db/tupleTypes  [:db.type/ref :db.type/long] ; ref to item, rank
 :db/cardinality :db.cardinality/many}

Or you could use a value type instead of a ref for item, if that works for you.

To use this in datalog, you can use the tuple and untuple functions.

Upvotes: 1

pete23
pete23

Reputation: 2280

The only answer I have - assuming that positioning is list dependent - is that you need to add an indirecting entity with a rank attribute. This isn't very pleasant. It would be nice if a many relation could be ordered, as this use case would simplify substantially.

Upvotes: 2

Related Questions