I have a python ETL script that published a shapefile as Feature Collection and then adds two CSV files as tables and establishes a one-to-many relationship between the spatial layer and the tables. The first related table works as expected -- features are returned via the `queryRelatedRecords` endpoint and I can configure popups with related record information in the Map Viewer.
The second related table always returns "No records found" when queried, even though it does contain related data.
Both tables use the same field in the relationship with the spatial layer and both are published using the same script code.
I've attached screenshots of the service directory information for the service and the relationship details for each layer. Also, I show the results of running the queryRelatedRecords for both relationship IDs which illustrates that only the first relationship (id: 0) returns values. A final screenshot shows the results of querying the related table directly to demonstrate that it does have records that match the district values.
Looking for suggestions on what I may have done wrong when creating the relationships via the Python APi, or if there are any additional ways to introspect the hosted services to find out why the second relationship is not working as expected.
def create_one_to_many(layer, table, parent_name, parent_key, child_name, child_key):
table_id = table.properties.id
layer_id = layer.properties.id
layer_relationship_count = len(layer.properties.relationships)
table_relationship_count = len(table.properties.relationships)
# Create the relationship between the layer and the table on the District field
layer_rel = {
"id": layer_relationship_count,
"name": f"{parent_name}_to_{child_name}",
"relatedTableId": int(table_id),
"cardinality": "esriRelCardinalityOneToMany",
"role": "esriRelRoleOrigin",
"keyField": parent_key
}
table_rel = {
"id": table_relationship_count,
"name": f"{child_name}_to_{parent_name}",
"relatedTableId": int(layer_id),
"cardinality": "esriRelCardinalityOneToOne",
"role": "esriRelRoleDestination",
"keyField": child_key
}
print(f"Layer Relationship added to {layer.properties.name}:", layer_rel)
res = layer.manager.add_to_definition({"relationships": [layer_rel]})
print(res)
if not res['success']:
print(f"Failed to add relationship to layer: {res.error}")
exit(0)
print(f"Table Relationship added to {table.properties.name}:", table_rel)
res = table.manager.add_to_definition({"relationships": [table_rel]})
print(res)
if not res['success']:
print(f"Failed to add relationship to table: {res.error}")
exit(0)