Transaction fail with relation

Thanks Mark.
Whats the appropriate call in this case? I cant find a childResualt(Singular).

customChildren: [myUser])

This returns Successful = true, but fails to actually create the relation in console.

Yes, the proper way is to convert the argument into an array. Could you please check what you get inside of the result object? Specifically, I’d like to see the individual result for the setRelation operation. You can see the structure of the result object documented here:
https://backendless.com/docs/ios/data_transactions_about_unitofwork.html#transaction-result

Regards,
Mark

  • key: “setrelationUsers1”
    ▿ value: <Backendless.OperationResult: 0x6000015b9530> #2
    • super: NSObject
    • operationType: Backendless.OperationType
      ▿ result: Optional(0)
      • some: 0 #3
        • super: NSNumber
          • super: NSValue
            • super: NSObject

Not sure if this is what you’re looking for

Yes, that’s what I am looking for, but I’d like to see the actual values in these objects.

For some reason I cant dump any further info.

two things came up though
the result has the key “setrelationUsers1”, is that 1 supposed to be there?

two
your link says the class is

public var operationType: OperationType?

// returns the result of the operation. The object type is determined
// by what the operation returned.
public var result: Any?

but the SDK is

    public var operationType: OperationType = .CREATE
   public var result: Any?

Yes, the name of the operation is setrelationUsers1, that’s expected.

Can you take a screenshot of the values in the response structure as you see them in the debugger?

The documentation describes the structure of the response. It contains two properties operationType and result.

         let y = x["setrelationUsers1"]! as OperationResult
        print(y.operationType.rawValue as Int)
        dump(y.result!)

Screen Shot 2020-07-24 at 12.45.17 PM

Every operation included in a transaction has its own result. For example, the result of the setRelation operation will be the number of objects set in the relation.

This is what I wanted to see - how many objects were set in the relation.

Gotcha, so am I doing something wrong or is there a bug?

I really am intrigued by the Transaction API and want to utilize it to the fullest on my new app.

I do not think there is a bug, we just need to find out what the result of that operation first :wink:

So the operation type of 8 is set relation
But the result is empty?

Is it possible to create a single object and create a 1:1 relation in a single transaction?

Yes, it is possible. Is the relation column declared in the table?

Also, could you try using the following signature instead?

unitOfWorkInstance.setRelation(parentTableName: String,
                               parentObjectId: String,
                               columnName: String,
                               childrenObjectIds: [String]) -> OpResult

where parentTableName is Users and parentObjectId will be the objectId property obtained from the Backendless.shared.userService.currentUser object.

Regards,
Mark

How do I get the children objectIDs?
The schema is complete.

Use the resolveToPropName method documented here:
https://backendless.com/docs/ios/data_transactions_operation_result.html#opresultvaluereference

The object in the docs identified as opResultInstance is going to be myUser in your app.

I couldn’t get that to work either.

This does not work, error message

Unable to perform ‘SET_RELATION’ operation due to argument incompatibility. The operation references a result from another ‘CREATE’ operation. The specified result cannot be obtained from this operation (list of objectIds).

        let uow = UnitOfWork()
        let myUser  = uow.create(objectToSave: getBEUserProperty())
         _ = uow.setRelation(parentObject: Backendless.shared.userService.currentUser!, columnName: kUserPropKey, childrenResult: myUser)
    
        uow.execute(responseHandler: { result in
         print("Is transaction successful - \(result.isSuccess)")
        print(result.error?.message)
       // dump(objectIdReference)

     }, errorHandler: { fault in
         print("Error: \(fault.message ?? "")")
     })

This DOES work

        let uow = UnitOfWork()

    let items = [getBEUserProperty()]
    let createOrderItemsResult = uow.bulkCreate(objectsToSave: items)
    _ = uow.setRelation(parentObject: Backendless.shared.userService.currentUser!, columnName: kUserPropKey, childrenResult: createOrderItemsResult)
    uow.execute(responseHandler: { result in
         print("Is transaction successful - \(result.isSuccess)")
       // dump(objectIdReference)

     }, errorHandler: { fault in
         print("Error: \(fault.message ?? "")")
     })

Have you tried the approach I suggested here?

that works if I manually enter a objectId I grab from console.

But why would Bulk be working and not a single object

I cant get it to work.
I don’t think there is a bug, but believe the functionality is missing to do what im trying to do.
Create a object,
Relate it to a parent on a 1:1 basis

Im just going to use the bulk method and call it good for now

@olhadanylova, please see the conversation above and create an example for Robert. Thanks!

Hello Robert,

According to the documentation this method

func setRelation(parentValueReference: OpResultValueReference,
                 columnName: String,
                 childrenResult: OpResult) -> OpResult

requires your children to be the result of the find or bulk create operation:

At the moment Swift-SDK doesn’t provide methods to set relations using the OpResult of single created object as a children parameter, so please use the bulkCreate method instead:

let uow = UnitOfWork()
let items = [getBEUserProperty()]
let createOrderItemsResult = uow.bulkCreate(objectsToSave: items)
_ = uow.setRelation(parentObject: Backendless.shared.userService.currentUser!, columnName: kUserPropKey, childrenResult: createOrderItemsResult)
uow.execute(responseHandler: { result in
    print("Is transaction successful - \(result.isSuccess)")
    // dump(objectIdReference)
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

I’ve created an internal ticket BKNDLSS-22315 to discuss this omission.

Regards,
Olha