建模数据中的多个多对多关系 [英] modelling multiple many-to-many relationships in datomic

查看:226
本文介绍了建模数据中的多个多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我仍然在思考sql,但我无法编写一个简单的博客的数据库模式。
我不太理解:db / cardinality 属性及其含义。






  • 系统支持多个用户


  • 每个用户可能有许多用户


  • 每篇文章可能有许多评论

  • 每个评论都有一个用户


解决方案

查看下图并在。他们在交易处内部运行,可以原子地强制执行任意约束,中止不符合他们的交易。


Maybe I'm still thinking sql but I'm having trouble writing the datomic schema for a simple blog. I don't really understand the :db/cardinality attribute and what it means.

In terms of this type of system, how do we model these relationships

  • The system supports multiple users
  • Each user may have many categories
  • Each user may have many articles
  • Each category may have many users
  • Each category may have many articles
  • Each article may have many comments
  • Each comment has one user

解决方案

Look at the following diagram and read the full code sample (schema, sample data and queries) at https://gist.github.com/a2ndrade/5651419. It should help you understand how to model data in Datomic.

Querying

Note that some relationships are not explicitly modeled because relationships in Datomic are bidirectional and because you can retrieve the rest of the information using simple Datalog queries. For example, the query:

(d/q '[:find ?cid ?c
   :in $ ?u
   :where
   [?uid :user/username ?u]
   [?aid :article/category ?cid]
   [?aid :article/author ?uid]
   [?cid :category/name ?c]]
 (d/db conn) "john.smith")

finds all the category ids -and their names- that a user ("john.smith") has written articles for.

Containment Relationships

An important modeling decision is to have articles point to comments and mark the relationship as :db/isComponent since comments should not exist on their own but as part of an article. Datomic will make sure to retract all comments associated with an article if the article itself is retracted.

Enforcing Business Rules

If you want to enforce application-specific consistency rules (e.g. articles and comments must have a author, comments must be of certain length, etc) you need to use database functions. They run inside the transactor and can atomically enforce arbitrary constrains, aborting transactions that don't comply with them.

这篇关于建模数据中的多个多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆