Using Search Cursor - RuntimeError: Cannot find field 'GlobalID'

1264
5
Jump to solution
03-03-2022 09:28 AM
KatHoenke
New Contributor

Hello. I have an fc that has been joined to a table. I am trying to use a search cursor and an insert cursor to identify where the rows differ and when they differ, insert them into a blank fc. 

For instance, here I am trying to identify where one field has values as null, insert them into the blank fc. I keep getting the following error, and cannot figure out what is going on, as there is a GlobalID field, and I am not even using it here. Appreciate the help!

 

#begin inserting features into the blank feature classes
searchfields = ["xy", "NIDID_1" , "xy"]
with arcpy.da.SearchCursor(NIDs_Joined,searchfields) as cursor:
    for row in cursor:
        #If the NIDIDs do not align, then this is a new point. Add it to the to_append fc
        if row[1] is None:
            curN.insertRow(row)
        #if the NIDIDs do match but the xy locations are different, add it to the change_loc fc
        if row[0] != row[2]:
            curL.insertRow(row)
Traceback (most recent call last):
  File "<string>", line 7, in <module>
RuntimeError: Cannot find field 'GlobalID'

 

0 Kudos
1 Solution

Accepted Solutions
DonMorrison1
Occasional Contributor III

I can't see where "GlobalId' is coming from but there is something odd about your searchfields list. Do you really want to read the same field ('xy') twice? I think you can do that but it seems redundant.  Will line 9 ever return True?  Perhaps 'xy' is an alias and not really the column name?

It might help to see how you create the curN and curL cursors.  For your code to work they must be created with the same number of list elements (3) as the search cursor.

View solution in original post

5 Replies
DonMorrison1
Occasional Contributor III

I can't see where "GlobalId' is coming from but there is something odd about your searchfields list. Do you really want to read the same field ('xy') twice? I think you can do that but it seems redundant.  Will line 9 ever return True?  Perhaps 'xy' is an alias and not really the column name?

It might help to see how you create the curN and curL cursors.  For your code to work they must be created with the same number of list elements (3) as the search cursor.

KatHoenke
New Contributor

Thank you! Essentially, I joined one dataset to an updated version of the same dataset, and calculated xy for both. I want to see if any of the new data xy is different than the old data xy_1, and if it is different, I want to add the row to a new fc (to update my master data). Here is the full code. Please let me know if this can be improved, I'm still learning a lot about how to make my scripts more efficient and better!

old_NID = r'F:\Analysis\InventoryWork\InventoryUpdateMay2020\Master_Datasets_Western_States\Western_Masters.gdb\NID2020_Schema'
#Join old NID schema to new NID schema
New_NID_join = arcpy.AddJoin_management(schemaone, "NIDID", old_NID, "NIDID", "KEEP_ALL")
NIDs_Joined = arcpy.CopyFeatures_management(New_NID_join, r'F:\Analysis\InventoryWork\InventoryUpdateMay2020\Master_Datasets_Western_States\Analysis\Web Service Work\NID_Versions.gdb\NIDs_Joined')
all_field_names = [f.name for f in arcpy.ListFields(NIDs_Joined)]
print(all_field_names)

# create 3 new fcs - one for new points, one for changing locations, one for changing attributes.

to_append = os.path.join(str(update_sets_gdb), "to_append")
change_loc = os.path.join(str(update_sets_gdb), "change_loc")
change_att = os.path.join(str(update_sets_gdb), "change_att")

#Identify points where locations have changed.
#Copy the joined FC three times and delete all features. These are the blank fcs for the features to be added into.
arcpy.CopyFeatures_management(NIDs_Joined, change_loc)
arcpy.CopyFeatures_management(NIDs_Joined, to_append)
arcpy.CopyFeatures_management(NIDs_Joined, change_att)
arcpy.DeleteFeatures_management(to_append)
arcpy.DeleteFeatures_management(change_loc)
arcpy.DeleteFeatures_management(change_att)

#create insert cursors to insert the features that meet these criteria
curL=arcpy.da.InsertCursor(change_loc,all_field_names)
curA=arcpy.da.InsertCursor(change_att,all_field_names)
curN=arcpy.da.InsertCursor(to_append,all_field_names)

#begin inserting features into the blank feature classes
searchfields = ["xy", "NIDID_1" , "xy"]
with arcpy.da.SearchCursor(NIDs_Joined,searchfields) as cursor:
    for row in cursor:
        #If the NIDIDs do not align, then this is a new point. Add it to the to_append fc
        if row[1] is None:
            curN.insertRow(row)
        #if the NIDIDs do match but the xy locations are different, add it to the change_loc fc
        if row[0] != row[2]:
            curL.insertRow(row)

 

0 Kudos
KatHoenke
New Contributor

I think that you figured it out - my cursors are pointing to the wrong field list. That fixed it! Thank you! Here is the updated script. 

 

searchfields = ["xy", "NIDID_1" , "xy_1"]
#create insert cursors to insert the features that meet these criteria
curL=arcpy.da.InsertCursor(change_loc,searchfields)
curA=arcpy.da.InsertCursor(change_att, all_field_names)
curN=arcpy.da.InsertCursor(to_append, searchfields)

#begin inserting features into the blank feature classes
with arcpy.da.SearchCursor(NIDs_Joined,searchfields) as cursor:
    for row in cursor:
        #If the NIDIDs do not align, then this is a new point. Add it to the to_append fc
        if row[1] is None:
            curN.insertRow(row)
        #if the NIDIDs do match but the xy locations are different, add it to the change_loc fc
        if row[0] != row[2]:
            curL.insertRow(row)

 

 

0 Kudos
KatHoenke
New Contributor

Is there a way to above, use only 3 fields to search but to insert rows with all fields?

I've updated my searchfields to include fields without globalid, but now I get the error that the workspace is already in transaction mode.

0 Kudos
DonMorrison1
Occasional Contributor III

If I understand what you are trying to do, I would consider a bit of a different approach like this (warning - I did not test this code):

lyr = arcpy.MakeFeatureLayer_management(NIDs_Joined , "temp_lyr1", "NLDID IS NULL")[0]
arcpy.CopyFeatures_management(lyr, to_append)

lyr = arcpy.MakeFeatureLayer_management(NIDs_Joined , "temp_lyr2", "xy <> xy_1")[0]
arcpy.CopyFeatures_management(lyr, change_loc)

 

0 Kudos