With the following code I try to identify for each team who is the oldest person in the team. It does not work. I have no experience with Datomic, so I am struggling a bit. I understand the documentation of Datomic so that I can use the max function inside a query and not only inside the :find expression. Is this correct, and does this also work in Datalevin? (using Datalevin 0.9.22)
(def my-db
(d/get-conn "test/svn_test_db" {:age {:db/valueType :db.type/long}}))
(d/transact my-db [{:name "Peter" :age 24 :team "a"}
{:name "Sandra" :age 46 :team "a"}
{:name "Hank" :age 23 :team "b"}
{:name "Emily" :age 2 :team "b"}])
(d/q '[:find ?team ?name ?highest-age
:where
[?oldest :name ?name]
[?oldest :team ?team]
[?oldest :age ?highest-age]
[?member :team ?team]
[?member :age ?age]
[(max ?age) ?highest-age]]
(d/db my-db))
=>
#{["a" "Peter" 24]
["b" "Emily" 23]
["b" "Hank" 23]
["a" "Sandra" 24]
["a" "Sandra" 46]
["a" "Peter" 46]
["b" "Hank" 2]
["b" "Emily" 2]}
I got it running by writing an extra function for it. It feels like a workaround. But it works well. Everything I found somewhere in the Internet (nested queries) I did not manage to get working.
(defn oldest-is [team age]
(-> (d/q '[:find (max ?age)
:in $ ?team-arg
:where
[?member :team ?team-arg]
[?member :age ?age]]
(d/db my-db) team)
first first (= age)))
(d/q '[:find ?team ?name ?highest-age
:where
[?oldest :name ?name]
[?oldest :team ?team]
[?oldest :age ?highest-age]
[(myns/oldest-is ?team ?highest-age)]]
(d/db my-db))It is a known issue https://github.com/juji-io/datalevin/issues/323 Will get around to it when I get a chance.