【问题标题】:Multiple user emails for multiple tag additions用于添加多个标签的多个用户电子邮件
【发布时间】:2015-03-20 12:29:34
【问题描述】:

在系统中,用户可以为现有条目添加标签,如下所示:

(defn add-tag-to-post [eid email tags]
  (let [cast-eid (Long. eid)]
       (d/transact conn [{:db/id cast-eid,
                          :author/email email,
                          :post/tag tags}])))

我认为,应该将电子邮件和标签添加到实体 ID 的交易中。


我希望能够查询此数据的所有更改,并查看哪个用户电子邮件添加了哪个标签。目前,当用户添加新标签时,所有电子邮件字段反映最多最近用户的电子邮件:

(defn get-all-post-history-by-eid [eid]
   (->> (d/q '[:find ?title ?content ?tags ?email ?eid
               :in $ ?eid
               :where
              [?eid post/title ?title]
              [?eid post/content ?content]
              [?eid author/email ?email]
              [?eid post/tag ?tags]] (d/db conn) eid)
         (map (fn [[title content tags email eid]]
              {:title title
               :content content
               :tags tags
               :email email
               :eid eid}))
         (sort-by :email)))

返回

({:title "Straight edges...", 
  :content "If ...", 
  :tags "art",                        <diff tag
  :email "randomlady@mailnator.hax",  <same email
  :eid 1759} 
 {:title "Straight edges ....", 
  :content "If ... ", 
  :tags "scissor-less edges",         <diff tag
  :email "randomlady@mailnator.hax",  < same email
  :eid 1759} 
 {:title "Straight edges ...", 
  :content "If ...", 
  :tags "paper",                      <diff tag
  :email "randomlady@mailnator.hax",  < same email
  :eid 1759} ... )

要点:所有不同的标签都是由不同的电子邮件添加的,但查询只是在所有字段中返回相同的电子邮件(为实体添加标签的最新电子邮件)

我只是将 schema.edn 中作者/电子邮件属性的序数更改为“许多”而不是一个,我认为这可能与此有关。但事实并非如此。

感谢任何帮助。谢谢=)


编辑:我在想也许我需要一个标签/作者字段,例如:

  post/tag      }
  author/email  } nothing tying these two together

+ tag/author    } does it make sense to add this?  will try and get back.

Edit#2:所以,我认为我在此页面上的文字的帮助下(认知上)取得了突破 http://tonsky.me/blog/unofficial-guide-to-datomic-internals/

基本上,Datomic 尊重 entity,这意味着如果您更改实体属性(好吧,当您为属性记录新值时,不会发生更改/删除),那么您实际上是在要求该属性的最新值。

这意味着对于添加的每个标签,创建一个新实体是合适的,并且在该实体中它可以引用它所调整的帖子。

所以从看起来像的东西

EID  Attribute      Value     "Time/Transaction ID"
 37  :post/title     "Bees"           7
 37  :author/email   "v@a.x"          7
 37  :post/tag       "hive"           7
 37  :author/email   "diana@p"        8
 37  :post/tag       "mind"           8

如果您更改属性 :post/tag 即使使用新的 :author/email,您仍在修改 ID 为 37 的实体,这将有效地“覆盖”这些值(好吧,标签是 ordinality/many 这样一个是附加的,但电子邮件会“覆盖”)

我的期望行为更像

EID  Attribute      Value     "Time/Transaction ID"
 37  :post/title     "Bees"           7
 37  :author/email   "v@a.x"          7


 38  :post/tag       "honeycomb"      8
 38  :author/email   "squire@o"       8
 38  :tag/post (ref)   37             8

意思是,ID为38的实体拥有所有新值,并成功指向/引用实体37。

写出问题很有帮助!一旦我得到实施,我会尝试更新这个问题。希望这对需要的人有所帮助。

【问题讨论】:

    标签: clojure datomic data-retrieval


    【解决方案1】:

    有效!

    (defn get-all-post-history-by-eid [pid]
      (->> (d/q '[:find ?title ?content ?tags ?email ?pid
                  :in $ ?pid
                  :where
                  [?pid post/title ?title]
                  [?pid post/content ?content]
                  [?tid tag/post ?pid]         <new schema stuff
                  [?tid tag/value ?tags]       <means new way to see the world
                  [?tid author/email ?email]] (d/db conn) pid)
           (map (fn [[title content tags email pid]]
                  {:title title
                   :content content
                   :tags tags
                   :email email
                   :eid pid}))
           (sort-by :email)))
    

    可以看到,查询中有新的字段,即:

    tag/post
    tag/value
    

    tag/post 在模式中具有 ref 的 valueType, tag/value 有 cardinality/many 并且是一个字符串。

    这样,标签和帖子就解耦了。现在,当我查询以获取类似帖子的所有历史记录时,

    vhax.dbmethods>  (get-all-post-history-by-eid 1759)
    

    结果是这样的

    ({:title "Straight edges without scissors", 
      :content "If you ever ... ", 
      :tags "skizzorz", 
      :email "so.ku@mail.hax", :bid 1759} 
    
     {:title "Straight edges without scissors", 
      :content "If you ever ...  ", 
      :tags "snips", 
      :email "vild@hax.pong", :bid 1759})
    

    反映任何电子邮件添加的标签。成功。真的觉得 Datomic 今天为我点击了。

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      相关资源
      最近更新 更多