Hey guys,
I’m very confused by the relationship system in Backendless (particularly the difference between a relationship created in a table and the opposite “child of” that is automatically created, and whether or not they work both ways), and i cant find a definite answer so far.
So, let’s say I have 2 tables:
-Shops
-ShopItems
A shop has a one-to-many relationship with shopItems (see attached). I have seen many examples of how to retrieve the list of shopItems given the shop, and it works well. But with that relationship, is it possible to retrieve a list of shops that sell a certain shopItem? I haven’t seen how to do this anywhere…
Thanks in advance !
Guillaume
Hello, Guillaume!
First about “child of”. It’s just a GUI representation of relation for the referenced object. You can use it to navigate set of parent items.
And about the second question about shops and items. You can retrieve list of shops by making request versus “Shops” table with whereClause like this ( relation name is “relatedItems” ): “relatedItems.objectId = ‘shoppingItemObjectId’”. So, you can create clauses using child table properties referencing them with syntax “<relationName>.<childTableProperty>”.
regards,
Alex
Thanks Alex ! i see how this works now. I’m able to retrieve the children the way you described.
But basically, the fact that one item is a “parent” and the other one is a “child” has no other implications right? They are just both linked by a bi-directional relationship?
Guillaume
I’m not sure that I understand what you mean under “bi-directional”)
Backendless client SDKs and docs are based on conception of “parents” and “children”. You can either search children by parents’ property or find parents by childs’ property. If it sounds like “bi-directional” for you - then yes, it is)
Lol yeah maybe I’m not very clear.
I’m wondering if “A is a parent of B” is equivalent to “B is a parent of A” (apart from the fact that you don’t retrieve parents and children the same way). Or is there something else involved (maybe reading rights or ownership or something?)
The underlying reason for my question is: we want to import our database from Parse and by default, Backendless structures it like “ShopItem is a parent of Shop”. (on our Parse database it is just: a ShopItem has a pointer to a Shop).
So, I’m wondering if its ok for us to go with this or if we should structure it the other way around ( that makes more sense to me) i.e. “Shop is a parent of ShopItem”?
Apologies if it’s not very clear; and thanks again for your help
Guillaume
Hi Guillaume,
A relationship is bi-directional, however, only one of the two tables is marked as “parent” which is the table where the relationship column is declared. For two tables, A and B, say A is the parent and B is the child. With that structure you can do the following:
for any given A, load all the child B objects. Here you can use either single-step or two-step relation retrieval.
for any given B, load the parent A object. In this case, you can find A with the following whereClause:
relationColumnNameFromA.objectId = 'objectId-of-B'
Hope this helps.
Regards,
Mark
Yes, everything is clear for me now. Thanks guys !