问题
I'd like to implement a many to many relationship which also has metadata describing the relationship. One could think of the relationship as a labelled edge. Specifically, a path consists of an ordered collection of series, and a series can be within more than one path, on each occasion having a position within such path. If I understand correctly some reification of the relationship is needed in datomic (as we cannot label edges directly), such as a join entity like:
:path/path-member ; ref, many
:path-member/series ; ref, one
:path-member/position ; long, one
Or to reify it more completely:
:path-member/series ; ref, one
:path-member/path ; ref, one
:path-member/position ; long, one
Are there any other data modelling options that could work?
Are composite attributes relevant here?
This question has been asked before but I wondered if any additions to datomic since that question was asked (2015) offer any new possibilities.
回答1:
Almost everyone encounters this question when they start data modelling with datomic, as evidenced by the large number of stack overflow questions on exactly the same point.
There is great news: Heterogenous tuples, added in June 2019, are a powerful new feature which solves this beautifully - it's exactly the feature we all thought was missing.
What it means is 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. While this length limitation does not go the full way to allowing an arbitrary amount of meta-data to be stored as labels on an edge, as in a true graph db, it adds great modelling power to datomic while retaining all the rest of the leverage and simplicity datomic provides.
Official blog post announcement.
Discussion of the release on twitter.
To use this in datalog, all you need are the tuple and untuple functions. It's beautifully simple, and is exactly the feature that was 'missing'.
来源:https://stackoverflow.com/questions/61386621/datomic-many-to-many-with-data-on-the-relationship