Suggestions on how to reverse relationship direction

I built my database with a relationship going one way between two tables. It now makes more sense to have it work the other way. There are ~35K rows that need to be updated.

I’m looking for suggestions on the best way to do this.

Thanks,
Tim

All relationships in Backendless are bi-rectional. When you declare a relationship in table A pointing to table B, you can still reference A object(s) from table B records. More on this is discussed in the following video:

Hi @mark-piller,

I am under the impression you can’t deep save “up” a relationship. I’m halfway through the deep save video, so maybe I will get to the point when that is discussed.

Thanks,
Tim

Hi Tim,

DeepSave works unidirectionally, the bidirectional nature applies only to data retrieval.

Regards,
Mark

That is what I thought. Do you have a suggestion on reversing the relationships so I can use deep save?

Thanks,
Tim

Unfortunately, there is no simple/automatic way to reverse the relationships. You would need to write a script (or Codeless logic) and rebuild the relationships with the API.

I was afraid that was going to be the answer.

As always, thanks for the help @mark-piller.

Best,
Tim

@mark-piller - Is it possible to go “up” a relationship for data retrieval? I don’t see anything in your video about this.

For example, I want to select 1 row from table B and include all parent relationships from table A as a property of the result.

I tried it in the rest console and got errors. I wonder if I have the syntax wrong.

Thanks,
Tim

Here’s an example:

I have table A and B. Table A has a one-to-many relationship to B:

Table A:

Table B:

Here’s REST Console for retrieving Bs with related A:

Hope this helps.

Mark

In your example, row B2 is returned twice for parent A2 and parent A3.

If you queried from A , for the row A2, and added a relationships to the query in the REST console (or API) for table B you would get the two “children” B2 and B3 nested (not sure what this is precisely called) in the results.

The REST console doesn’t allow parent relationships (it is greyed out in your example), and I assume the API is the same.

To write out what I want -

  1. get row B2 from table B (WHERE name = ‘B2’)
  2. get any rows in table A that are parent rows to row B2
  3. return those as a relationships list of Table A objects as a property of B2

Could be some typos in here -

[
 {
  "name": "B2",
  "objectId": "xxxxxx",
  "A": [
          {
            "name": "A2",
            "objectId": "xxxxxx"
         },
        {
            "name": "A3",
            "objectId": "xxxxxx"
         }
       ]
  }
]

That would be a fully reversible parent/child relationship, right?

I’m not trying to nit pick, I just want to make sure this isn’t possible so I can move on.

Tim

You cannot get a collection of related objects located “up the stream” along with a collection of the primary objects. You can retrieve specific properties of the parent object and if there are multiple parent objects, they will be replicated in additional records. This is how database work, we’re simply following the pattern.

Thanks, @mark-piller. I’m coming from old-school DB work where relationships were maintained by numeric IDs and other was no way to nest results so it takes me a little longer to understand what can and can’t be done.

Tim