IndexError and corrupted hosted feature service when using the ViewManager create method

Open
#2,537 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
api, backend

Research direction

Start with arcgis/features/managers.py, especially create_view and set_visible_fields_and_query, then follow the ViewManager.create entry point in arcgis/gis/init.py. Reproduce the traceback with a multi-layer hosted feature service using query and visible_fields, and compare the service definition before and after. Done means view creation completes without IndexError and the parent service still exposes its feature layers.

Written by the indexing model from the issue text.

Description

bug

Describe the bug
The create method in the ViewManager class throws an IndexError when using the query or visible_fields parameters, for hosted feature service containing more than one feature layer.

Removing the query and visible_fields parameters also throws the IndexError after successfully creating just one view from the parent hosted feature service.
It seems to corrupts the parent hosted feature service definition in some way(s), where the layers are no longer recognized via the sharing REST endpoint and in the view creation/update UI.
See screenshots for examples of expected versus corrupted.

To Reproduce
Steps to reproduce the behavior:

# example parameters
view = {
      "name": "Sanitary Lift Stations view",
      "hfl_itemid": sanitary_hfl_itemid,
      "layer_id": 0,
      "vis_field": ["*"],
      "filter": "Status <> 'Abandoned' AND StructureType = 'Lift Station'"
  }

view_item = flyr_item.view_manager.create(
                        name=new_view_name, 
                        view_layers=[flyr_item.layers[view['layer_id']]], 
                        allow_schema_changes=False, 
                        updateable=False, 
                        capabilities="Query", 
                        preserve_layer_ids=True, 
                        query=view['filter'], 
                        visible_fields=["OBJECTID", "GlobalID"] + view['vis_field']
                    )

error:

Traceback (most recent call last):
  File "C:\Users\<path to .py>", line 140, in <module>
    main()
    ~~~~^^
  File "C:\Users\<path to .py>", line 75, in main
    view_item = flyr_item.view_manager.create(
            name=view_name,
    ...<5 lines>...
            query="Status <> 'Abandoned' AND StructureType = 'Lift Station'" if view_name == "Sanitary_Lift_Stations_view" else "Status <> 'Abandoned' AND (StructureType = 'Outfall' OR StructureType = 'Grass Swale Outfall')"
        )
  File "C:\Users\AStJohn\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\arcgis\gis\__init__.py", line 20804, in create
    return mgr.create_view(
           ~~~~~~~~~~~~~~~^
        name=name,
        ^^^^^^^^^^
    ...<14 lines>...
        query=query,
        ^^^^^^^^^^^^
    )
    ^
  File "C:\Users\AStJohn\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\arcgis\features\managers.py", line 3110, in create_view
    set_visible_fields_and_query(item, visible_fields, query, gis)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AStJohn\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\arcgis\features\managers.py", line 3078, in set_visible_fields_and_query
    fields = item.layers[0].properties["fields"]
             ~~~~~~~~~~~^^^
IndexError: list index out of range

Screenshots
After running the view create() method, with OR without the query and visible_fields parameters, the UI no longer recognizes feature layers and only tables are available when creating or updating the view.

Image Image

Expected behavior
This API call has been working for over a year in our scripts, creating hundreds of views programmatically. I expect that the views be created with query and visible_fields parameters defined and that the parent hosted feature service is not altered in any detrimental way.

Platform (please complete the following information):

  • OS = Windows 11
  • Browser = Chrome
  • Python API Version = 2.4.2

Additional context
Add any other context about the problem here, attachments etc.

Dominant language
Python
Stars
2.2k
Forks
1.1k
Avg merge
2h 40m
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Esri/arcgis-python-api

All issues in Esri/arcgis-python-api

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.