which is just what I needed. And it works with join as well
(select [widgets 'a]
(from '[vats c])
(join '[widgets b] (= :a.batch :b.batch))
(fields :a.id)
(where (= :a.source :c.id)))
This is terrific! But I think this should be in the documentation somewhere,
as it wasn't apparent and is not an uncommon situation.
One small issue that this raised, however, was that when using a vector entity
as the first argument to select/select*, the :ent field of the resulting query becomes
a
vector of maps, not a single map. As a result when
korma.core/apply-transforms is called during
exec, the transforms field is not found and any transforms on the
unaliased entity are not applied. (Version 0.3.0-RC5.)
Admittedly, it is unclear what transformations to apply with complex joins
or if it even makes sense to do so given the combination of data from
different tables. And in cases where transforming does make sense, like
simple self joins, one can often work around this by not aliasing the main table.
So perhaps this is a feature not a bug.
But the transforms
are applied (using the transforms for the main table)
when there are joins used
without aliasing, which would seem to raise the same
concerns as above. This appears inconsistent, leading to different results
based on a tangentially related action (aliasing) by the user.
Should transforms be disallowed with joins or allowed with aliasing?
If the latter, something like the following appears to work:
(defn- apply-transforms
[query results]
(if (= (:type query) :delete)
results
(if-let [trans (seq (-> query :ent
(#(if (vector? %) (get % 0) %))
:transforms))]
(let [trans-fn (apply comp trans)]
(if (sequential? results)
(map trans-fn results)
(trans-fn results)))
results)))
in korma.core, with one line added, which extracts the first entity
in the vector case.
Thanks! I've been very happy with Korma so far.