Problem while update relation between two tables

Hello!

I’m developing an app with 3 tables: “PuntiLuce”, “Circuiti”, “Users”.

There is a one to one relation between PuntiLuce and Circuiti, in detail one object from PuntiLuce can have only one Circuito object related, but the same Circuito object can be related with many PuntiLuce objects.

When I try to update this relation changing the PuntiLuce.circuito value with another Circuito object, I get that now the PuntiLuce object is related with both the old Circuito object and the new one, as it was one to many relation. This happen every time I try to update the PuntiLuce object, from both console or app (iOS).

I’ve attached some screenshot to help to understand my situation. What am I doing wrong?

Thanks in advance for your help!

Hello,

It is pretty much impossible to digest any information from the provided screenshots. Please include complete screenshots showing the entire console screen. Also, please show the code you use to update relations.

Regards,
Mark

Thank for the quick response. The code I’m using is this:

func getPuntiLuce(completionHandler:@escaping ([PuntiLuce]?)->()) ->() {
    
    let dataStore = Backendless.sharedInstance()?.data.of(PuntiLuce().ofClass());
    let dataQuery = BackendlessDataQuery()
    dataQuery.queryOptions.pageSize = kMaxPageSize
    dataQuery.queryOptions.sortBy = ["matricola"]
    dataQuery.queryOptions.related = ["geopoint.coordinates", "circuito.name"]
				
    dataStore?.find(dataQuery, response: { (response) in
        guard let puntiLuce = response?.getCurrentPage() as? [PuntiLuce] else {
            completionHandler(nil)
            return
        }
        
        completionHandler(puntiLuce)
        }, error: { (fault) in
            print("FAULT")
            completionHandler(nil)
            
    })
}






var puntoLuce = puntiLuce[0] // array got using the above function


puntoLuce.circuito = newCircuito // of class Circuiti


let dataStore = Backendless.sharedInstance()?.data.of(PuntiLuce().ofClass());


dataStore?.save(puntoLuce, response: { (response) in
    if let pl = response as? PuntiLuce {
        print("SAVED")
        
    }else{
        print("ERROR")
    }
    }, error: { (fault) in
        print("FAULT")
})

Using the above code I success updating the puntoLuce object, and the column circuito has the expected value. The problem is that I still can query this object using the circuito old value, furthermore if I delete from the console the new relation, I see that this object is still related with the old circuito value. Eventually if I delete also this relation from the console, finally the circuito value is empty.
I hope I made it clearer.

Hi Santo,
If I understand the case correctly, you have one relation columnA from table A to table B (one to one) and one relation columnB from table B to A (one to many). Which makes two different columns total.

If you change the value of columnA, it doesn’t affect the value of columnB in any way. This is why when you update the one to one relation, the other one to many relation doesn’t get update. You shall need to update both of the relations.

Hi Sergey,

I have created only one relation (one to one) from table A to table B (“circuito” in the first screenshot), I see in the console that a one to many relation from B to A has been created automatically (“PuntiLuce.circuito figlio di” in the screenshot 2), but I cannot access, edit or delete this column (screenshot 3), neither through the app. I think the problem is around this column but I don’t know how to solve it and what is actually causing this.

This is actually neither a one to many relation nor a column, just a kind of indication of parent objects.

How do you retrieve PuntiLuce objects having a Circuito object?

func getPuntiLuceFor(circuito: Circuiti, completionHandler:@escaping ([PuntiLuce]?)->()) ->() {
    let dataStore = Backendless.sharedInstance()?.data.of(PuntiLuce().ofClass());
    let dataQuery = BackendlessDataQuery()
    dataQuery.queryOptions.pageSize = kMaxPageSize
    dataQuery.queryOptions.sortBy = ["matricola"]
    dataQuery.queryOptions.related = ["geopoint.coordinates", "circuito.name"]
    dataQuery.whereClause = "circuito.objectId = '\(circuito.objectId)'"
    
    dataStore?.find(dataQuery, response: { (response) in
        guard let puntiLuce = response?.getCurrentPage() as? [PuntiLuce] else {
            completionHandler(nil)
            return
        }
        completionHandler(puntiLuce)
        }, error: { (fault) in
            print("FAULT")
            completionHandler(nil)
    })
}

After you update PuntiLuce object with some new Circuito do you see the change on data service console? Can you please make a screenshot before and after update?

In the “Screenshot before update” you can see that is linked with the old Circuiti object, then I updated the relation as you can see in “Screenshot after update 1” and “Screenshot after update 2”, but if I query with the old Circuito object I still find the PuntiLuce object as shown in the “Screenshot after update 3”

Can I have your application ID, please?