Search code examples
aqueduct

Is it possible to modify the properties that are returned for the joined objects that have WHERE clause?


I have been trying to change the returned properties of a joined object, considering that it has a filter from a WHERE clause. However this was not possible directly because the WHERE clause returns a QueryExpression class and not the Query class that has the returningProperties method.

I tried according to the code below:

var query = Query<Obj1>(context)
    ..where((o) => o.state).equalTo(state);
    query.join(set: (q) => q.objs2)
    ..where((a) => a.state).equalTo(state)
    ..returningProperties((a) => [a.id, a.description, a.answer]);
  var results = await query.fetch();

I was able to perform the query doing the following:

var query = Query<Obj1>(context)
    ..where((o) => o.state).equalTo(state);
    Query<Obj2> subQuery = query.join(set: (q) => q.objs2)
    ..where((a) => a.state).equalTo(state)
    ..returningProperties((a) => [a.id, a.description, a.answer]);
  var results = await query.fetch();

My question is, is there another way to do this? What have I done is correct? Is it possible to have a simpler or more direct way of putting these clauses together?


Solution

  • Not sure what correct is without knowing the intent of the query. Both of the queries you've listed are identical as far as I can tell; based on the error message you provided, you may have previously had an error that you fixed (the first where was missing equalTo).

    The query you have here finds all Obj1s whose state is state. Then it joins the objs2 for each, but only the Obj2 whose state is state. The returned Obj2 have only their id, description and answer populated.

    I'd suggest using the latter of your two examples - where you extract the subquery into a variable. It makes the query easier to modify, and lots of intermixed dot and cascade operators make code hard to understand.