3

I have A -> AB <- B many to many relationship between 2 ManagedObjects (A and B), where AB is the junction table.

When querying A from db, how do i join B values to AB joint objects?

Query<A> query = await Query<A>(context)
    ..join(set: (a) => a.ab);

It gives me a list of A objects which contains AB joint objects, but AB objects doesn't include full B objects, but only b.id (not other fields from class B).

Cheers

  • Hi Andres, you should provide enough code and information so others can reproduce your problem. Can you post the definition of your two ManagedObjects? – Vlastimil Ovčáčík Aug 22 '18 at 17:35

1 Answers1

4

When you call join, a new Query<T> is created and returned from that method, where T is the joined type. So if a.ab is of type AB, Query<A>.join returns a Query<AB> (it is linked to the original query internally).

Since you have a new Query<AB>, you can configure it like any other query, including initiating another join, adding sorting descriptors and where clauses.

There are some stylistic syntax choices to be made. You can condense this query into a one-liner:

final query = Query<A>(context)
    ..join(set: (a) => a.ab).join(object: (ab) => ab.b);
final results = await query.fetch();

This is OK if the query remains as-is, but as you add more criteria to a query, the difference between the dot operator and the cascade operator becomes harder to track. I often pull the join query into its own variable. (Note that you don't call any execution methods on the join query):

final query = Query<A>(context);
final join = query.join(set: (a) => a.ab)
  ..join(object: (ab) => ab.b);
final results = await query.fetch();
Joe Conway
  • 1,566
  • 9
  • 8