问题
I realise that we don't need ordinary pivot tables in datomic because we can just pick one side of the relationship and put a :db.cardinality/many
attribute on there (being sure to model the relationship in one direction only).
The next thing I encounter in converting from sql is a pivot table that also stores an attribute on the relationship, like:
series_subscriptions
user_id int
series_id int
expires_at timestamp
I was wondering what the options are to model this in datomic. I notice there are some things in schema that I don't understand the reason for yet, or appropriate usage, like composite tuples, components.
One option though is elevate the pivot to its own entity so it can handle the extra attribute:
:series-subscription/user ; ref, one
:series-subscription/series ; ref, one
:series-subscription/timestamp ; timestamp, one
But the above seems a bit wrong. Doing it like that means in datalog the join would no longer be implicit, which would appear to be a loss. Wonder if there are better options.
回答1:
Heterogeneous tuples are the more concise way to achieve this since their introduction in July 2019.
See the answer to this question.
The July 2019 update to Datomic contained a lot of different features all at once and usage is not yet well documented online, though the docs do explain it (not much is needed to explain it - a case of simple but, perhaps, not easy). Note it's specifically heterogeneous tuples that solve the concern here, it would work to ignore the other types of tuples to get started for this use case.
来源:https://stackoverflow.com/questions/61064412/how-to-represent-pivot-table-with-extra-data-on-it-in-datomic