# Autoincrement field when New Feature added

I know that this is probably a very basic programming but I am brand new to Python.

I have a layer in a geodatabase that I need a unique ID field (for joining). I can't use the ObjectID because that can change. I can't use a GlobalID because the data will be used by databases that can't handle the "{" brackets or dashes.

I have already populated the field. When I add a new feature I want to have the value of the max existing value + 1 for the new feature.

Thanks to Jason here is what I have so far-

import arcpy
gdb_full_path = r'D:\GIS\ZA.gdb\Current'
feat = 'ZA_Merge'
arcpy.env.workspace = gdb_full_path
with arcpy.da.SearchCursor(feat,["ANID"]) as cursor:
for row in cursor:
unique_id_counter = row.count
with arcpy.da.InsertCursor(feat,["ANID"]) as cursor:
cursor.insertRow((unique_id_counter + 1))


Here is the error that I get -

Runtime error Traceback (most recent call last): File "", line 9, in TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'int'

-
Are you working with ArcGIS? If so, which version? –  Jason Apr 18 '13 at 20:48
Yes...sorry. ArcGIS 10.1 –  Ken B Apr 18 '13 at 20:51

Here is a basic example of how you can add a new feature to a feature class with an incremental unique ID based on the last feature in said feature class:

import arcpy

gdb_full_path = r'c:\full\path\to\your\geodatabase.gdb'
feat = 'feature_class'

arcpy.env.workspace = gdb_full_path

# create a search cursor so we can read the rows of the feature class
with arcpy.da.SearchCursor(feat,['name_of_unique_id_field']) as cursor:
for row in cursor:
# for each row, we read the unique ID...once we get to the end our
# new variable, "unique_id_counter", will be equal to the value of the
# last feature
unique_id_counter = row.name_of_unique_id_field

# create an insert cursor to add new features to the feature class
with arcpy.da.InsertCursor(feat,['name_of_unique_id_field']) as cursor:
# insert a new row and populate the unique ID field with a new value
cursor.insertRow( (unique_id_counter+1) )

-
nice and smart! –  MC5 Apr 18 '13 at 21:22
Error that I get- Runtime error Traceback (most recent call last): File "<string>", line 7, in <module> AttributeError: 'tuple' object has no attribute 'ANID' –  Ken B Apr 18 '13 at 22:08
That field exists and I had to tweek a little because I was getting the same problem with this line - with arcpy.da.SearchCursor(feat,['name_of_unique_id_field']) as cursor: –  Ken B Apr 18 '13 at 22:12
@KenB, update your question and include the code that you are using. It looks like you are trying to access data from a field that wasn't read by the cursor object we created. When we create the cursor objects in the example above, we are specifying that the cursor only returns the fields in the list ['name_of_unique_id_field']. You can drop this list entirely and the cursor will return all fields - this way you can access data from other fields. The main advantage of returning specific fields is seen when you have very large tables with many fields that you might not need for a given case. –  Jason Apr 18 '13 at 22:32
@Jason, Sorry, just getting used to the formaatting. –  Ken B Apr 18 '13 at 23:06

if the data are in ArcSDE, you could use the RDBMS platform, whether it be SQLServer, Oracle, etc, to use a sequence to populate an ID column with a unique auto-incremented integer identifier. if you are using shapefiles or file geodatabase, you might have to write an Editor extension that responds to insert events.

-
Actually, in response to MC5's answer, I am going through a similar issue and found that ESRI does not support IS Identity = Yes on spatial, versioned data. You will receive an error stating "cannot insert explicit value for identity column in table when identity_insert is set to off". This has been mentioned in the ESRI Forums as well... forums.arcgis.com/threads/…. ~MB –  MLB Mar 10 at 14:30
I didn't mean to suggest tweaking the OBJECTID column - let ArcSDE do its magic there, and ignore it forevermore. Add a second ID column you manage yourself, such as with a sequence. If you need to do inserts into a feature class from plain SQL and you want the OBJECTID column to increment, see the sde.version_user_ddl.next_row_id() function, at least in Oracle. –  MC5 Mar 10 at 17:05