I’m trying to find lessons
in my schema which belong to the same series
and which are of higher rank
than the given lesson. But the below query is returning all lessons of higher rank, regardless of which series they belong to. I’m somewhat lost as to why this is the case.
Below, :lesson/series is a ref & :lesson/rank is an integer.
(defn higher-siblings [l-idf]
"Return id and rank of higher siblings."
(d/q '[:find ?sibling ?rank
:in $ l
:where
[?l :lesson/rank ?r]
[?l :lesson/series ?s]
[?sibling :lesson/series ?s]
[?sibling :lesson/rank ?rank]
[(> ?rank ?r)]]
(d/db conn) l-idf))
My problem: the query seems to be binding ?sibling
regardless of the ?s
of ?l
.
Intuitively that suggests an ‘and’ is missing, but can’t quite reconcile that with the fact that since these are logic variables, I thought ‘and’ should be implied in some sense.
Is this anything to do with the fact that I have multiple where clauses referencing the same ‘a’ value?
Just for interest, I also have a composite tuple to make rank+series unique, so another form, which gives the same result, is:
'[:find ?sibling ?rank
:in $ l
:where
[?l :lesson/rank+series ?tup]
[(untuple ?tup) [?r ?s]]
[?sibling :lesson/series ?s]
[?sibling :lesson/rank ?rank]
[(> ?rank ?r)]]