Backendless Support
 
Answered

Delete related children

Hello

If I have Table A that has 1:N relation to Table B

Is there a way that I can delete all the objects in Table B that are related to certain object in Table A

The way I can think of it is cumborsome:

1- Get all the related rows from Table B by quering the relation on Table !

2- Delete the rows from Table B

3- Delete the relation from Table A

Is there a shorter way where I can say:

Delete all the rows in Table B whose parent in Table A has object id xyz

OR

Delete the relation and its rows from Table A where object is xyz

Thank you

Best Answer
photo

In that case, the API call would look like this:

Backendless.Data.remove( "Address", "Person[addresses].objectId = 'AAAA-BBBB-1" )

Leave a Comment

Comments (7)

photo
1

Hi SnakeEyes,

Yes, there is exactly such a request - a bulk delete on table B with where clause "A[relationToB].objectId='xyz'". As soon as the related object(s) is deleted, the relation is also removed - you don't have to break it manually if one of the objects disappears.

Here's a relevant doc on bulk delete: https://backendless.com/docs/rest/doc.html#deletingseveralobjectsatatimebulkdelete

Though apparently it's only available via REST now.

photo
1

Thank you. I am using android sdk for my android app. Is this not availble there?

photo
1

Yes, there is (not documented yet though):

Backendless.Data.remove( "tableName", "whereClause" )

photo
1

Thanks Mark and Sergey. I am just not sure how the syntax would be done in the use case. I can see based on the syntax of Backendless.Data.remove( "tableName", "whereClause" ) that the where clause will apply on columns on the TableName (such as amount >0, text = "John" ..etc). But what don't see how it can be used on another table relating to the table in question. Let's use real column name to illustrate.

Suppose we have 2 tables. Person and Address.

Person table has column "addresses" which is 1:N to the table Address.

I want to delete all the addresses for the person with ObjectId= "AAAA-BBBB-1"

How can this be achieved using the remove method?

Thank you

photo
1

Hello , any update on my query above?Thank you!

photo
1

In that case, the API call would look like this:

Backendless.Data.remove( "Address", "Person[addresses].objectId = 'AAAA-BBBB-1" )

photo
1

Thank you. Btw the api I found was

Backendless.Data.of(String table).remove(string where)I tried it and it worked

photo