0

I am trying to use an Update Cursor to copy a GlobalID field into a string field. However, I keep getting an error saying "Objects in this class cannot be updated outside an edit session [Medians]".

Aren't I initiating as edit session by using arcpy.da?

The error occurs on the fourth line.

workspace = "x/y/z"
edit=arcpy.da.Editor(workspace)
cursor = arcpy.da.UpdateCursor(medians, ("GlobalID", "GlobalID2",))
for row in cursor:
    globalID = row[0]
    globalID = globalID[1:-1]
    row[1]=globalID
    cursor.updateRow(row)
2
  • What is your workspace and what is the feature class you are trying to edit? Is it the complete code you run? Does your feature class participate in any kind of geodatabase behaviour (geometric network, topology, network dataset)? Commented May 28, 2015 at 15:15
  • Hi Alex, I appreciate your response. I've edited my code to a point where it works (see below). My feature class is in a file gdb on a network drive. The feature class had an existing relationship class, so maybe that was the issue? I've seen elsewhere that this error arises from a versioning problem, but I don't know if this is why I got the error, as my feature class is not in an sde.
    – Olivia
    Commented May 28, 2015 at 16:03

1 Answer 1

3

I've found a solution. I'm not sure entirely why, but changing the code as follows gets rid of the problem:

import arcpy
import os

medians = "file.gdb/Medians"
workspace = os.path.dirname(medians)

edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()

with arcpy.da.UpdateCursor(medians, ("GlobalID", "GlobalID2")) as cursor:
    for row in cursor:
        globalID = row[0]
        globalID = globalID[1:-1]
        row[1] = globalID
        cursor.updateRow(row)

edit.stopOperation()
edit.stopEditing(True)

This is the thread that I followed to get this code.

2
  • Just creating an Editor isn't enough. The startEditing method is used to start an edit session and the startOperation method is used to start an edit operation. Required steps in the process. Of course, you could use the Editor with a with statement, then those steps happen auto-magically.
    – DWynne
    Commented May 28, 2015 at 16:29
  • thanks olivia this worked for me on a similar issue (using updateCursor on SDE fc)
    – atxgis
    Commented Aug 22, 2017 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.