Question

I'm trying to do a join across a number of tables (three plus a join table in the middle). I think korma is lazily evaluating the last join. What I'm trying to do is to add a condition that restricts the results of the first table in the join, but I'm only interested in fields from the last table in the join.

So for example say I've got clubs, people and hobbies tables, and a people-to-hobbies join table for the last two. Each club can have many people, and each person can have many hobbies.

I'm trying to get the full details of all the hobbies of people who belong to a specific club, but I don't want any fields from the club table. The join table means that korma will create two queries, one to get all the people that are in a specific club, and another to retrieve the hobbies for that person via the people-to-hobbies join table.

My korma query looks something like this:

(select clubs
  (with people
    (with hobbies
      (fields :hobby-name :id)
      (where {:clubs.name "korma coders"}))))

The problem is that I haven't specified which fields I want from clubs and people, and the default is to select *. How can I include no fields from these tables? Is this possible, or does the fact that the hobbies are lazily loaded mean korma has to return some results in the first query (which gets me a filtered list of people), so that when I come to interrogate it later for the hobbies, it has the ids it needs to run the second query?

Was it helpful?

Solution

I'd use join macro in such case:

(select clubs
  (join people)
  (join people-to-hobbies)
  (join hobbies)
  (fields :hobbies.hobby-name :hobbies.id)
  (where {:clubs.name "korma coders"}))))

It's a bit more explicit, but as an additional benefit it's run with a single query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top