1

I have the ObjectBox database and it is working pretty well. But nested queries not working as expected

List<Channel> _channels({required String lang}) {
    final queryBuilder = _channelBox().query();
    queryBuilder.linkMany(
        Channel_.category, ChannelCategory_.lang.equals(lang));
    queryBuilder.linkMany(Channel_.name, ChannelName_.lang.equals(lang));
    final query = queryBuilder.build();
    final result = query.find();
    query.close();
    //
    return result;
  }

This query returns all languages although the filter.

A unit test to demonstrate this

test("Channels for lang should have one language only", () {
    DB.languages.forEach((lang) async {
      (await db.channels(lang: "ar").first).forEach((channel) {
        channel.category.forEach((cate) {
          expect(cate.lang, "ar");
        });
        channel.name.forEach((name) {
          expect(name.lang, "ar");
        });
      });
    });
  });

here is how I defined my entities

@Entity()
class Channel {
  @Id()
  int id = 0;
  String url;
  ToMany<ChannelName> name = ToMany();
  ToMany<ChannelCategory> category = ToMany();
  bool isFavorite = false;    
}

@Entity()
class ChannelName {
  @Id()
  int id = 0;
  String name;
  String letter;
  String lang;
  @Backlink()
  final ToMany<Channel> channel = ToMany();
}

@Entity()
class ChannelCategory {
  @Id()
  int id = 0;
  String name;
  String lang;
  @Backlink()
  final ToMany<Channel> channel = ToMany();
}
  • The code looks fine to me. Can you share a small example/unit test that fails? Otherwise can't really reproduce or tell what the issue is. – Uwe - ObjectBox Feb 28 '22 at 07:39
  • Thanks! But I actually can't run it like this. E.g. what data is inserted? Maybe you can help diagnose your issue by using `print(query.describeParameters());` (see https://docs.objectbox.io/queries#debug-queries). – Uwe - ObjectBox Mar 01 '22 at 15:04
  • Thanks so much for your response. Here is a link for a sample https://github.com/muhammadalkady/test_objectbox I have tried many ways but no luck. – Muhammad Al-kady Mar 03 '22 at 11:56
  • All tests except one in the example work. And the test "Favorite channels should be 1 item in first category" fails, because actually two channels are part of Category.channel with language ar. The condition `linkMany(Category_.channel, Channel_.favorite.equals(true)` is only used to match `Category` objects. The `Category` object will still return all objects that are part of its `channel` ToMany, these are not filtered. – Uwe - ObjectBox Mar 07 '22 at 10:02
  • Can I filter sub-objects or this is normal behavior? – Muhammad Al-kady Mar 07 '22 at 12:11
  • It's "normal" behavior that all data of an object is returned. However, with the obtained `Category` ID one could maybe write an additional query on the `Channel` box. – Uwe - ObjectBox Mar 08 '22 at 07:03

0 Answers0