I used the ArcGIS API for Python (code below) to add a table to a hosted feature service, as well as adding a relationship between the new table and an existing feature layer in the service. The new table is visible in the REST page for the Feature Service, but I neither see it in the item description page (Overview or Data tabs) nor can I add it to a map by constructed URL. Was there a step or parameter I missed in uploading it with Python?
bf_table = r"Path/BrownfieldsInventory"
fields_def = []
for f in arcpy.ListFields(bf_table):
field_dict = {}
field_dict['name'] = f.name
field_dict['type'] = "esriFieldType" + f.type
field_dict['alias'] = f.aliasName
field_dict['length'] = f.length
field_dict['nullable'] = f.isNullable
field_dict['editable'] = f.editable
fields_def.append(field_dict)
tbl_definition = {
"id": 12,
"type" : "Table",
"name" : "Brownfields",
"description": "Description",
"fields": fields_def,
"indexes": [
{
"name": "PK_IDX",
"fields": "OBJECTID",
"isAscending": True,
"isUnique": True,
"description": "clustered, unique, primary key"
}
],
"objectIdField": "OBJECTID",
"uniqueIdField": {
"name": "OBJECTID",
"isSystemMaintained": True
}
}
item = gis.content.get("existingFeatureServiceID")
flc = FeatureLayerCollection.fromitem(item=item)
status = flc.manager.add_to_definition(json_dict={"tables": [tbl_definition]})
Solved! Go to Solution.
I remember experiencing a similar issue in the past, and I had to modify the script to use "add_to_definition" as well as "item.update" to get things to show in both the REST service as well as the item details page.
Maybe try adding something like this after using "add_to_definition"?
item.update(data={
"layers": [dict(layer.properties) for layer in flc.layers],
"tables": [dict(table.properties) for table in flc.tables]
})In case of it's of interest, here is the code I used to add a relationship:
holdings_to_brownfields = [
{
"id": 8,
"name": "Brownfields",
"relatedTableId": 12,
"cardinality": "esriRelCardinalityOneToMany",
"role": "esriRelRoleOrigin",
"keyField": "TTN",
"composite": False
}]
brownfields_to_holdings = [
{
"id": 8,
"name": "Holdings",
"relatedTableId": 1,
"cardinality": "esriRelCardinalityOneToMany",
"role": "esriRelRoleDestination",
"keyField": "TTN",
"composite": False
}
]
relationship_lyrs = {
"Holdings" : holdings_to_brownfields,
}
relationship_tbls = {
"Brownfields" : brownfields_to_holdings
}
for fl_name, relationship_def in relationship_lyrs.items():
print(fl_name)
fl = [fl for fl in item.layers if fl.properties.name == fl_name][0]
status = fl.manager.add_to_definition({"relationships" : relationship_def})
print(status)
for tbl_name, relationship_def in relationship_tbls.items():
print(tbl_name)
tbl = [tbl for tbl in item.tables if tbl.properties.name == tbl_name][0]
status = tbl.manager.add_to_definition({"relationships" : relationship_def})
print(status)
I remember experiencing a similar issue in the past, and I had to modify the script to use "add_to_definition" as well as "item.update" to get things to show in both the REST service as well as the item details page.
Maybe try adding something like this after using "add_to_definition"?
item.update(data={
"layers": [dict(layer.properties) for layer in flc.layers],
"tables": [dict(table.properties) for table in flc.tables]
})This did the trick, thank you so much! Sort of interesting that the item object has to be updated separately after an operation on the service definition like adding tables, I'm trying to think of a scenario where it would ever be desirable to add to the definition but not update the item as well?