0

I have read the object box documentation for many-to-many relationship https://docs.objectbox.io/relations

And also this solution for many-to-many relationship in noSQL firebase Many to Many relationship in Firebase

What could be the issue if I go with second approach for all 1:N and M:N relations for my offline database using Dart objectbox library?

Adding some code for 1:N relationship

class Customer {
  int? id;
}

class Orders {
  int? id;
  int? customerId;
}

So here every order will always have an extra customer id. Whenever we would require to find the orders for a customer, that will happen via querying the orders for customer id.

raphire
  • 198
  • 9
  • what do you mean by "the second approach"? – vaind Jul 21 '21 at 07:45
  • Second approach means this https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebase For instance, for one to many relation ship between class A and class B Class A { int id } Class B { int id int classAId; } – raphire Jul 21 '21 at 15:59
  • There are multiple different approaches in the linked SO page. If you wanted to link to a specific answer, you can do so getting the link from the "Share" button (or from the time/date for a specific comment) – vaind Jul 22 '21 at 07:09
  • And also, because the linked page can change, it's best practice on StackOverflow to copy-paste the relevant piece of code - helps people answer your question – vaind Jul 22 '21 at 08:37
  • @vaind Apologies for the confusion. I have added some code for the 1:N relationship. I hope that makes the question clear for 1:N relationship. For M:N relationship here is the link https://stackoverflow.com/a/41528908/6429262 – raphire Jul 22 '21 at 09:55
  • @vaind is the question clear now? – raphire Jul 26 '21 at 12:18

1 Answers1

1

So here every order will always have an extra customer id. Whenever we would require to find the orders for a customer, that will happen via querying the orders for customer id.

Yes, that makes sense, that's how ObjectBox ToOne relation actually works internally (it stores an ID).

Therefore, for the greatest ease-of-use with ObjectBox, you could define your model this way:

@Entity()
class Customer {
  int? id;
}

@Entity()
class Orders {
  int? id;
  final customer = ToOne<Customer>();
}

You could of course keep the plain ID but then you wouldn't be able to make use of query .link() feature to make the connection. Query link() allows you to have queries across multiple entities, for example, get orders for customers matching some criteria (e.g. all orders for customers from a single city, assuming you stored the address).

vaind
  • 1,642
  • 10
  • 19
  • Okay thanks, for the answer. How about M:N relationship? Here instead of having `ToMany()` relationship, we could have a separate class that will store the relationships between two class. For querying, first we will have to do query on the relationship class to find the IDs and then do second query using those IDs. – raphire Jul 28 '21 at 13:01
  • Yes, you could do that but I'm not sure why you would want to? Again, that's exactly what ToMany does in objectbox under the hood, albeit more efficiently. OK there is one reason where it would make sense - if you needed to attach additional information (fields) on the to-many relationship class - for example a DateTime field when it was added. In such a case it would make sense. In any case, with the separate class, you need to do the saving manually (as opposed to being done "automatically" by the ToMany class). – vaind Jul 28 '21 at 14:00
  • Thanks for the answer. That is one case. In our case, we are struggling to read/update the entity data with relationships. It's like for each entity we have to manually assign the relationship using a loop. It would be better if adding data to an entity with relationship would be independent of relationship itself and there would be class level one time relationship declaration. We were thinking of maintaining separate classes to bypass this. But I think it would be as tedious as the normal approach. – raphire Aug 02 '21 at 13:56
  • Not sure I follow, maybe try opening a new SO question with a short code example – vaind Aug 02 '21 at 15:25
  • "> adding data to an entity" You can just read an existing object (`box.get()`), change what you need, then write it back (`box.put()`) - no need to touch relations if you don't change them – vaind Aug 03 '21 at 10:03