How To: Add a relationship class to a hosted feature layer using ArcGIS API for Python
Summary
A relationship class can be added to a hosted feature layer in ArcGIS Online through the ArcGIS REST Administrator Directory. Users can add and delete relationship classes in a hosted feature layer in ArcGIS Online if the related table and the feature layer exist within the same layer. However, it is not recommended to edit the layer through the REST endpoint because this can modify the schema of the hosted feature layer.
Another alternative to adding relationships to a hosted feature layer is via ArcGIS API for Python. This article describes the workflow to add a relationship class in ArcGIS Online using Python to automate the process. The script is run using a standalone .py file or ArcGIS Notebooks.
Procedure
- Import the necessary modules.
from arcgis import GIS
- Connect to the ArcGIS Online account.
agol = GIS("https://www.arcgis.com", "username", "password")
- Access the hosted feature layer and retrieve the item ID. Replace FL_ITEM_ID with the item ID.
item = agol.content.get("FL_ITEM_ID")
- Specify the feature layer and the layer ID. Replace '0' with the proper layer index if there is more than one layer.
lyr = item.layers[0] lyr_id = lyr.properties.id
- Specify the table and table ID. Replace '0' with the proper table index if there is more than one table. Alternatively, the relationship can also be added with a different layer.
#Relationship with table tbl = item.tables[1] tbl_id = tbl.properties.id #Relationship with another layer lyr2 = item.layers[1] lyr2_id = lyr2.properties.id
- Create a dictionary containing the relationship properties for the feature layer. Replace LYR_FIELD with the name of the matching field in the layer. In the example below, a one-to-one cardinality is used. Alter it accordingly for an alternate cardinality.
lyr_rel_dict = { "name": "Layer_to_Table", "relatedTableId": int(tbl_id), "cardinality": "esriRelCardinalityOneToOne", "role": "esriRelRoleOrigin", "keyField": "LYR_FIELD", "composite": True
- Create a dictionary containing the relationship properties for the table. Replace TABLE_FIELD with the name of the matching field in the table. In the example below, a one-to-one cardinality is used. Alter it accordingly for an alternate cardinality.
tbl_rel_dict = { "name": "Table_to_Layer", "relatedTableId": int(lyr_id), "cardinality": "esriRelCardinalityOneToOne", "role": "esriRelRoleDestination", "keyField": "TABLE_FIELD", "composite": True
- Update the relationship properties in the layer and table.
lyr.manager.add_to_definition({"relationships" : [lyr_rel_dict]}) tbl.manager.add_to_definition({"relationships" : [tbl_rel_dict]})
The code block below demonstrates the sample full script.
from arcgis import GIS agol = GIS("https://www.arcgis.com", "username123", "password123") item = agol.content.get("FL_ITEM_ID") lyr = item.layers[0] lyr_id = lyr.properties.id tbl = item.tables[0] tbl_id = tbl.properties.id lyr_rel_dict = { "name": "Layer_to_Table", "relatedTableId": int(tbl_id), "cardinality": "esriRelCardinalityOneToOne", "role": "esriRelRoleOrigin", "keyField": "TestField", "composite": True } tbl_rel_dict = { "name": "Table_to_Layer", "relatedTableId": int(lyr_id), "cardinality": "esriRelCardinalityOneToOne", "role": "esriRelRoleDestination", "keyField": "TestField", "composite": True } lyr.manager.add_to_definition({"relationships" : [lyr_rel_dict]}) tbl.manager.add_to_definition({"relationships" : [tbl_rel_dict]})
Related Information
- ArcGIS Pro: Relationship class properties
- ArcGIS Pro: Create Relationship Class (Data Management)
- ArcGIS API for Python: Layer
- ArcGIS API for Python: Item
Last Published: 1/26/2023
Article ID: 000028794
Software: ArcGIS Online Current