HOW TO

Define a field as unique using ArcGIS API for Python

Last Published: July 3, 2024

Summary

Unique values symbolize qualitative categories of values and can be based on one or more attribute fields in the dataset. When data are published to ArcGIS Online or Portal for ArcGIS, unique constraints can be added to a field to ensure no duplicate values are stored. This method is useful in identifying unique features, and improves labeling. It also helps stop operations such as appending or updating that add duplicate values into the fields. Constraints can be added by accessing the Data page of the hosted feature layer. It can also be added using ArcGIS API for Python and this article provides the workflow.

Procedure

  1. Import the necessary modules and specify the credentials.
from arcgis.gis import GIS
from arcgis.features import FeatureLayer, Table, FeatureLayerCollection

#For ArcGIS Online
gis = GIS("ORGANIZATION_URL", "username", "password")

#For ArcGIS Notebook
gis = GIS("Home")

#For Portal for ArcGIS
gis = GIS("https://<machine>.<domain>.com/<web_adaptor_name>", "username", "password", verify_cert=False)
  1. Specify the hosted feature layer to be used.
featurel_url = "https://services.domain.com/<xxxxxxxxxxx>/arcgis/rest/services/<feature_layer>/FeatureServer/<sublayer_id>"

fl = FeatureLayer(fl_url, gis)
  1. Add the index and definition to set the unique values in the desired field. Ensure the isUnique parameter is set to True.
definition_to_add = {"indexes":[
    {
        "fields": "<field_name>", 
        "isUnique": True, 
        "description": "description"
    }
]}

fl.manager.add_to_definition(definition_to_add)

The code block below shows the full working script for Portal for ArcGIS.

from arcgis.gis import GIS
from arcgis.features import FeatureLayer, Table, FeatureLayerCollection

gis = GIS("https://machine.esri.com/arcgis", "username123", "password123", verify_cert=False)

featurel_url = "https://services.domain.com/xxxxxxxxxxx/arcgis/rest/services/testUnique/FeatureServer/0"

fl = FeatureLayer(fl_url, gis)

definition_to_add = {"indexes":[
    {
        "fields": "<field>", 
        "isUnique": True, 
        "description": "description"
    }
]}

fl.manager.add_to_definition(definition_to_add)

Article ID: 000032984

Software:
  • ArcGIS Online
  • Portal for ArcGIS
  • ArcGIS API for Python 1 x
  • ArcGIS Enterprise 11 0
  • ArcGIS Enterprise 11 1
  • ArcGIS Enterprise 11 3
  • ArcGIS Enterprise 11 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options