Add unique id's to multiple feature classes using update cursor

89
5
Jump to solution
04-06-2020 07:47 AM
NataliaGutierrez1
New Contributor III

Hello!

I need to add unique ID's to a specific field in multiple feature classes that are inside a geodatabase.

I have created the code below but it is not working.

When I run, it I get the following error message:
            Traceback (most recent call last):
            File "D:/APRX_MXDS/USA_Parcels_2019_Project/Scripts/UniqueID_2.py", line 15, in <module>
            for row in cursor:
            RuntimeError: A column was specified that does not exist.

            Process finished with exit code 1

The error is referring to a column that does not exist.

Is column the same as field?

The field "UNIQUE_ID_PYTHON" was created before running the code, and so it should run.

import arcpy
import uuid

# Script to add unique Id's
featureGDB = r"D:\APRX_MXDS\USA_Parcels_2019_Project\test_featureclasses.gdb"
arcpy.env.workspace = featureGDB
arcpy.env.overwriteOutput = True

List_fc = arcpy.ListFeatureClasses("*single") # get a list of feature classes that end with the word single.
List = ["UNIQUE_ID_PYTHON"] # Field where I want to add the unique ids

for fc in List_fc: # loop through each feature class in the list and add unique ID to the specified field.
with arcpy.da.UpdateCursor(fc, List) as cursor:
for row in cursor:
row[0] = '{' + str(uuid.uuid4()) + '}'
cursor.updateRow(row)

Not sure how to fix this.

Can somebody guide me ?

Thank you

Reply
0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Esteemed Contributor

Is column the same as field?  Yes.

 

To verify the existence of a given field in your feature class, use the arcpy.ListFields() method.  Just be aware that this method returns a list of field objects so as you iterate through the list you'll need to access the particular object property:

for field in arcpy.ListFields(yourFeatureClass):
print(f'{field.name} {field.type}')
‍‍‍‍‍‍‍‍

Another couple of questions: when you added the field Unique_ID_Python did you add it as a guid type?  Also, are you sure you want to use a guid as the id and not something else? For example: Create Database Sequence—Data Management toolbox | Documentation 

Also, not sure what you are trying to get done with this line:

row[0] = '{' + str(uuid.uuid4()) + '}'
can't wait to retire....

View solution in original post

5 Replies
JoeBorgione
MVP Esteemed Contributor

Is column the same as field?  Yes.

 

To verify the existence of a given field in your feature class, use the arcpy.ListFields() method.  Just be aware that this method returns a list of field objects so as you iterate through the list you'll need to access the particular object property:

for field in arcpy.ListFields(yourFeatureClass):
print(f'{field.name} {field.type}')
‍‍‍‍‍‍‍‍

Another couple of questions: when you added the field Unique_ID_Python did you add it as a guid type?  Also, are you sure you want to use a guid as the id and not something else? For example: Create Database Sequence—Data Management toolbox | Documentation 

Also, not sure what you are trying to get done with this line:

row[0] = '{' + str(uuid.uuid4()) + '}'
can't wait to retire....

View solution in original post

JoeBorgione
MVP Esteemed Contributor

Just took a look at How To: Calculate unique identifier values similar to Global IDs  so that is what your line 15 is doing.  May I suggest that rather than an update cursor, just use the calculate field method instead

can't wait to retire....
Reply
0 Kudos
NataliaGutierrez1
New Contributor III

Thank you Joe!

The problem was with the data type of my field.

I did not know that I had to set it as GUID.

Once I changed it my code ran without errors.

Reply
0 Kudos
JoeBorgione
MVP Esteemed Contributor

There is a significant difference in a guid type of field and adding Global IDs; the former is like any other attribute, where you have control over the value.  Where as the latter are maintained by the geodatabase.  Your guid fields should transfer between databases just fine but you need to jump through a hoop or two to transfer/preserve Global IDs....

can't wait to retire....
Reply
0 Kudos
NataliaGutierrez1
New Contributor III

thank you for the explanation!

Reply
0 Kudos