This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-16
Channels
- # adventofcode (36)
- # announcements (30)
- # babashka (1)
- # beginners (161)
- # boot-dev (2)
- # bristol-clojurians (3)
- # clj-kondo (10)
- # clojure (125)
- # clojure-europe (10)
- # clojure-italy (4)
- # clojure-nl (7)
- # clojure-spec (1)
- # clojure-uk (26)
- # clojuredesign-podcast (3)
- # clojurescript (13)
- # core-async (5)
- # cryogen (3)
- # cursive (134)
- # data-science (8)
- # datascript (3)
- # datomic (32)
- # fulcro (24)
- # graalvm (2)
- # joker (5)
- # leiningen (5)
- # malli (18)
- # off-topic (14)
- # pathom (4)
- # re-frame (3)
- # reagent (11)
- # remote-jobs (3)
- # rewrite-clj (8)
- # shadow-cljs (47)
- # spacemacs (3)
- # sql (12)
- # vim (6)
Hi, folks. I'm new to DataScript, there's one confuse me while learning. I saw :db/id
is used at entity-id and all the relations are built upon this. But in some case, the data from server (backed by sql) often looks like :user/id
, that is not a global unique id. And this user/id
is used for relationship. How can I simplify here?
@doglooksgood if I understand correctly, I’d believe you’d model this by using a schema like:
{:user/id {:db/unique :db.unique/identity}}
Now you can transact some users:
(d/transact! conn [{:user/id 123}, {:user/id 789}])
Later, if you need to build relations on user 123, you can do so using lookup refs. For example, let’s say we have a :user/best-friend
relation, and user 123’s best friend is user 789:
(d/transact! conn [{:user/id 123, :user/best-friend [:user/id 789]}])
Notice how you don’t actually need to use raw :db/id
in order to specify which entities you’re interested in; you can use their unique attributes instead.
Keep in mind that :db/id
itself is not globally unique either. It’s an entity id that is local to a specific DataScript database. It’s up to you to design a schema that includes such an attribute. In the example above, you could transact UUIDs as the values for the :user/id
attribute if global uniqueness is a requirement.
Hope this helps. Also, it is not recommended to set the :db/id
yourself, as it can be seen as an internal implementation detail of the database.
Further, for modeling the relations of “external systems”, I like to use namespaced keyword attributes that include that system name in the namespace part. For example:
:facebook.user/id
, :twitter.user/id
, etc, Users in your system can then possess any combination of these unique attributes.