Transaction Returning Error

I am running into an issue when I am doing a transaction that first creates a nested object that I then want to relate to its parent. Here is my rest body

{
    "isolationLevelEnum": "SERIALIZABLE",
    "operations": [
        {
            "operationType": "CREATE",
            "table": "ItemExpense",
            "opResultId": "createItemExpense",
            "payload": {
                "expense_name": "${expense_name}",
                "purchase_price": "${purchase_price}",
                "unit_of_measurement": "${unit_of_measurement}",
                "quantity_of_unit": "${quantity_of_unit}"
            }
        },
        {
            "operationType": "CREATE",
            "table": "CreationLog",
            "opResultId": "saveInLog",
            "payload": {
                "object_created": {
                    "___ref": true,
                    "opResultId": "createItemExpense",
                    "propName": "objectId"
                },
                "table_name": "ItemExpense"
            }
        },
        {
            "operationType": "ADD_RELATION",
            "table": "Item",
            "opResultId": "addItemExpenseRelationToItem",
            "payload": {
                "parentObject": "${parent_object_id}",
                "relationColumn": "item_expenses",
                "unconditional" : {
                    "___ref": true,
                    "opResultId" : "saveInLog",
                    "resultIndex" : 1
                }
            }
        }
    ]
}

What I get back is this error:

{
  "success": false,
  "error": {
    "operation": {
      "operationType": "ADD_RELATION",
      "table": "Item",
      "opResultId": "addItemExpenseRelationToItem",
      "payload": {
        "conditional": null,
        "unconditional": {
          "___ref": true,
          "opResultId": "saveInLog",
          "resultIndex": 1
        },
        "parentObject": "B3201532-5B98-47C0-907E-021CBB86FEEB",
        "relationColumn": "item_expenses"
      }
    },
    "message": "Unable to perform 'ADD_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)."
  },
  "results": null
}

Not sure what the issue is.

Hello @Joshua_Chestang,

According to the documentation

This operation (ADD_RELATION) expects a collection of child objects. This means the referenced operation result is one that is returned by either the Retrieving objects or the Saving multiple objects operations.

I can advise you to save the CreationLog object using the CRETE_BULK operation and add the relation this way:

{
	"isolationLevelEnum": "SERIALIZABLE",
  	"operations": [
		{
			"operationType": "CREATE",
			"table": "ItemExpense",
        	"opResultId": "createItemExpense",
        	"payload": {
        		"expense_name": "exp1",
            	"purchase_price": "100"
        	}
    	},
      	{
       		"operationType": "CREATE_BULK",
          	"table": "CreationLog",
          	"opResultId": "saveInLog",
          	"payload": 
          	[
            	{
              		"table_name": "ItemExpense",
                  	"object_created": {
                    	"___ref": true,
                    	"opResultId": "createItemExpense",
                    	"propName": "objectId"
                	}
				}
           	]
		},      	
      	{
    		"operationType": "ADD_RELATION",
    		"table": "Item",
    		"opResultId": "addItemExpenseRelationToItem",
    		"payload": {
        		"parentObject" : "C91AC746-A3EE-4E02-8C39-834E3E4198DD",
        		"relationColumn" : "item_expenses",
        		"unconditional" : {
            		"___ref":true,
            		"opResultId": "saveInLog"
        		}
    		}
		}  
	]
}

Regards,
Olha

1 Like

Thank you so much. This definitely leads me in the right direction. Although the code you suggested didn’t work, I was able to use your suggestion of turning CREATE in CREATE_BULK.

This is my final code:

{
    "isolationLevelEnum": "SERIALIZABLE",
    "operations": [
        {
            "operationType": "CREATE_BULK",
            "table": "ItemExpense",
            "opResultId": "createItemExpenses",
            "payload": [{
                "expense_name": "${expense_name}",
                "purchase_price": "${purchase_price}",
                "unit_of_measurement": "${unit_of_measurement}",
                "quantity_of_unit": "${quantity_of_unit}"
            }]
        },
        {
            "operationType": "ADD_RELATION",
            "table": "Item",
            "payload": {
                "parentObject": "${parent_object_id}",
                "relationColumn": "item_expenses",
                "unconditional" : {
            		"___ref":true,
            		"opResultId": "createItemExpenses"
        		}
            }
        }
    ]
}

Does your code work for you?

the code you suggested didn’t work

sorry, there could be some inaccuracies in tables scheme reproduction.