How to represent pivot table (with extra data on it) in datomic?

 ̄綄美尐妖づ 提交于 2020-06-01 05:29:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!