Relationship update methods and their effects
When you create a relationship between two objects, the API request you use determines which scripts are triggered and when derived properties are recalculated.
The following sections explain the effects of the four different ways you can update a relationship. For example, to create a relationship between a planet object and a moon object, you can either update the object directly or POST to the relationship endpoint. The method you choose has important implications for server-side processing.
Method 1: Update the source object directly
You can create a relationship by sending a PATCH or PUT request to the "source" of the relationship. In this example, the planet object is the source.
PATCH managed/planet/saturn
[{ "operation" : "add", "field" : "/moons/-", "value" : { "_ref" : "managed/moon/titan" } }]
// Alternatively, a PUT request with the fully-updated moons array.
Effects
-
Scripts on the source object (
planet): Thefield policy,onValidate, andonUpdatescripts are invoked.[1] -
Scripts on the target object (
moon): Scripts aren’t invoked. -
RDVP calculations: Recalculations for relationship derived virtual properties (RDVPs) on the target object (
moon) depend on notification settings. If a recalculation is triggered, themoonobject’sfield policy,onValidate, andonUpdatescripts are then invoked.
Method 2: Update the source object’s relationship endpoint
You can also create the relationship by sending a POST request to the relationship endpoint on the source object.
POST managed/planet/saturn/moons?_action=create
{ "_ref" : "managed/moon/titan" }
Method 3: Update the target object’s relationship endpoint
Similarly, you can POST to the relationship endpoint on the target object.
POST managed/moon/titan/planet?_action=create
{ "_ref" : "managed/planet/saturn" }
Method 4: Update the target object directly
Finally, you can create the relationship by sending a PATCH or PUT request to the "target" of the relationship.
PATCH managed/moon/titan
[{ "operation" : "add", "field" : "/planet/-", "value" : { "_ref" : "managed/planet/saturn" } }]
// Alternatively, a PUT request with the fully-updated planets array.
Effects
-
Scripts on the source object (
planet): Scripts aren’t invoked. -
Scripts on the target object (
moon): Thefield policy,onValidate, andonUpdatescripts are invoked.[1] -
RDVP calculations: Recalculations for RDVPs on the source object (
planet) depend on notification settings. If a recalculation is triggered, theplanetobject’sfield policy,onValidate, andonUpdatescripts are then invoked.