UpdateCursor on versioned SDE data failing

113
3
Jump to solution
08-10-2022 08:39 AM
NicoleV
New Contributor

I am attempting to update utility feature classes with an update cursor. I have several layers I am practically doing this exact thing for and would really like to get it to work but I am stumped.

I have read a few other posts about this issue but they all seem to be older and without applicable solutions to what I am doing. I also found a post stating that this is a known bug/limitation of the update cursor, but is again from several years ago so I'm just not sure if this bug hasn't been fixed yet or if there is something else I am missing.  There is no information on the UpdateCursor help documentation about the bug that I have come across.

I first tested all my code on copies of the sde layers that I put into a testing gdb and those all ran without error. However, now that I'm at the point of wanting to implement the script on my production data, I am getting the following error on line 32:

TypeError: cannot update the table

 

Any help or additional insight would be greatly appreciated!

Nicole

# WORKSPACE FOR EDIT SESSION
workspace = r"C:\Workspace\Default.sde"
# ASSIGN VARIABLE TO SUBDIVISION LAYER
outputSub = r"C:\org.sde\Subdivision"
# ASSIGN VARIABLE TO UTILITY LAYER
outputManhole = r"N:\Workspace\Default.sde\Manhole"

# SPATIAL JOIN SELECTION FC OF SUBDIVISIONS TO UTILITY FEATURES
arcpy.SpatialJoin_analysis(outputManhole, outputSub, outsubSJManhole, "JOIN_ONE_TO_MANY", "KEEP_ALL", "", "CLOSEST", 3000, "DIST")

# CONVERT MM/dd/yyyy TO yyyy ONLY
convertInstall = arcpy.ConvertTimeField_management(outsubSJManhole, "INSTALLDATE", "", "YEAR", "TEXT", "yyyy")

# ASSIGN VARIABLE TO OPEN EDIT SESSION
edit = arcpy.da.Editor(workspace)

# START EDIT SESSION
edit.startEditing(False, False)

# START EDIT OPERATION
edit.startOperation()

# TABLE JOIN OF ORIGINAL UTILITY LAYER TO CONVERTED DATETIME TABLE
InstallJoin = arcpy.AddJoin_management(outputManhole, "FACILITYID", convertInstall, "FACILITYID", "KEEP_ALL")

# ASSIGN VARIABLE TO DICTIONARY
# LIST COMPREHENSION TO FILL DICTIONARY USING SEARCH CURSOR
# KEY IS FACILITY ID TO MATCH RECORDS FROM SPATIAL JOIN
InstallDict = {r[0]: (r[1:]) for r in arcpy.da.SearchCursor(InstallJoin, ["Manhole.FACILITYID", "outsubSJManhole.YEAR"])}

# UPDATE CURSOR IN WITH STATEMENT
with arcpy.da.UpdateCursor(outputManhole, ["FACILITYID", "INSTALLDATE", "YEARBUILT"]) as cursor:
    # FOR EVERY ROW BEING PULLED FROM TABLE
    for row in cursor:
        # IF 'INSTALLDATE' HAS A VALUE
        if row[1] != None:
            # KEY VALUE(FACILITY ID) IN DICTIONARY IS SAME AS FACILITY ID 
              FIELD IN UPDATE CURSOR FIELD
            keyValue = row[0]
            # IF KEY VALUE (FACILITY ID) IS IN THE DICTIONARY
            if keyValue in InstallDict:
                # 'YEARBUILT' IS EQUAL TO THE MATCHING DICTIONARY VALUE 
                   BASED ON FACILITY ID
                row[2] = InstallDict[keyValue][0]
                # CALL TO UPDATE THE ROW WITH THE VALUE
                cursor.updateRow(row)
# DELETE DICTIONARY
del InstallDict

 

0 Kudos
1 Solution

Accepted Solutions
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Think you need to set the editor to True for versioned data:

 

 

edit.startEditing(False, True)

 

 

Edited- grabbed the wrong description previously.

From the docs:

When False, you have full control of editing a nonversioned, or versioned dataset. If your dataset is nonversioned and you use stopEditing(False), your edit will not be committed (otherwise, if set to True, your edits will be committed).

(The default value is True)

 

View solution in original post

0 Kudos
3 Replies
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Think you need to set the editor to True for versioned data:

 

 

edit.startEditing(False, True)

 

 

Edited- grabbed the wrong description previously.

From the docs:

When False, you have full control of editing a nonversioned, or versioned dataset. If your dataset is nonversioned and you use stopEditing(False), your edit will not be committed (otherwise, if set to True, your edits will be committed).

(The default value is True)

 

0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

For clarification because that doc snippet is convoluted, from their example:

# Set multiuser_mode to False if working with unversioned enterprise gdb data
edit.startEditing(with_undo=False, multiuser_mode=True)

and dont forget the closing:

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(save_changes=True)

 

NicoleV
New Contributor

Thank you so much! I knew it would be something small that I overlooked and just needed a fresh pair of eyes. Changing it to True worked! 

The false was just left over from when I was using it for my testing gdb. I did have the stopOperation and stopEditing in my script, I just forgot to include in my snippet so thank you for clarifying as well.

0 Kudos