Through the admin REST API, I've had some success creating 1:M relationships for existing hosted feature services. Why not just overwrite the service you ask? There is a bug during sd file creation in ArcMap, making republishing not an option.
I start out by updating the layer definition for the layer with the origin field via - https://services.arcgis.com/<Organization ID>/arcgis/admin/services/<ServiceName>.FeatureServer/<layer ID>/addToDefinition In this case, the layer id is 0 and the origin field will be GlobalID. Here is what I add to the definition:
{
"relationships" : [
{
"id" : 0,
"name" : "Layer1sInLayer0",
"relatedTableId" : 1,
"cardinality" : "esriRelCardinalityOneToMany",
"role" : "esriRelRoleOrigin",
"keyField" : "GlobalID",
"composite" : false
}
]
}The second layer has an id of 1 and the destination is a GUID field named "RelatedGUID"
This is what I add to the layer definition:
{
"relationships" : [
{
"id" : 0,
"name" : "Layer1sInLayer0",
"relatedTableId" : 0,
"cardinality" : "esriRelCardinalityOneToMany",
"role" : "esriRelRoleDestination",
"keyField" : "RelatedGUID",
"composite" : false
}
]
}After running these two, the relationship appears valid in the ArcGIS online map viewer and Collector for ArcGIS.
Some things I've noticed:
- Must be done on both layers separately. I Could not add both layer parts to the definition of the entire feature service.
- Sometimes deleting relationships works. Other times I am unable to delete from the definition with a message saying a specific contraint doesn't exist. I do not see any sql server constraints visible from the REST service directory.
- It doesn't seem as though relationship names or id's need to match for both layers.
- Duplicate id numbers can be added to a layer and WILL BREAK IT!
- I've only tested GlobalID to GUID but assume Integer to Integer would work.
- I have not tested setting composite to true
- I have not tested many to many
- I have not had luck manipulating relationships with updateDefinition
My question for anyone reading: Is there another way to add/modify/delete relationships to existing services? I highly doubt this is supported and there isn't any REST API documentation.
Hopefully AGOL admins find this helpful. Relationships can be so powerful in a mapping application. It's too bad that we have to go to such extremes to apply them to hosted services when you can simply right-click create relationship class on the data behind ArcGIS Server services. Maybe enough people will break their services and we'll get a tool to manipulate hosted relationships!