edit session and CalculateField_management

3010
4
10-08-2015 10:38 AM
JordanParfitt
Occasional Contributor

I keep getting the message "Objects in this class cannot be updated outside of an edit session." Can anyone tell me what I'm doing wrong?

   def CreateRelationshipClasses(self):

        #  Set workspace
        env.workspace = "C:\Users\jsparfitt\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\uppco@neptune.sde"
       
        try:
            #  Go through list of relationship classes defined in variables.py and create them in file gdb

            for rc in relClassList:
                WriteToReport("Building " + rc["relClassName"] + " relationship class ...")

                if (rc["addCalcKeyFields"]):
                   
                    #  ESRI BUG: Need to add string field to hold globalid values for both feature class and table
                    #            Relationship classes between globalid and guid fields fail in Python
                       
                    WriteToReport("Adding " + rc["originPrimaryKey"] + " field to " + rc["originTable"] + " ...")
                    arcpy.AddField_management(rc["originTable"], rc["originPrimaryKey"], "TEXT", "", "", 50)

                    WriteToReport("Adding " + rc["originForeignKey"] + " field to " + rc["destinationTable"] + " ...")
                    arcpy.AddField_management(rc["destinationTable"], rc["originForeignKey"], "TEXT", "", "", 50)

                    WriteToReport("Calculating " + rc["originPrimaryKey"] + " field values ...")
                   
                    # Start an edit session. Must provide the worksapce.
                    edit = arcpy.da.Editor(env.workspace)

                    # Edit session is started without an undo/redo stack for versioned data
                    #  (for second argument, use False for unversioned data)
                    edit.startEditing(False, True)
                    arcpy.CalculateField_management(rc["originTable"], rc["originPrimaryKey"],
                          "!" + rc["realPrimaryKey"] + "!", "PYTHON")

                    WriteToReport("Calculating " + rc["originForeignKey"] + " field values ...")
                   
                    edit.CalculateField_management(rc["destinationTable"], rc["originForeignKey"], 
                         "!" + rc["realForeignKey"] + "!", "PYTHON")

                #  Create relationship class

                arcpy.CreateRelationshipClass_management(rc["originTable"],
                                                         rc["destinationTable"],
                                                         rc["relClassName"],
                                                         rc["relClassType"],
                                                         rc["forwardLabel"],
                                                         rc["backwardLabel"],
                                                         rc["msgDirection"],
                                                         rc["cardinality"],
                                                         rc["attributed"],
                                                         rc["originPrimaryKey"],
                                                         rc["originForeignKey"]
                                                         )
        except Exception, e:
            print e.message

(Curtis Price formatted code, see Posting Code blocks in the new GeoNet​)

0 Kudos
4 Replies
JordanParfitt
Occasional Contributor

I tried this too:

    edit = arcpy.da.Editor(env.workspace)  
    edit.startEditing(False, True)  
    edit.startOperation()  
    with arcpy.da.Editor(env.workspace) as fieldEdit:  
        WriteToReport("Calculating " + rc["originPrimaryKey"] + " field values ...")
        arcpy.CalculateField_management(rc["originTable"], rc["originPrimaryKey"], 
            "!" + rc["realPrimaryKey"] + "!", "PYTHON")
        WriteToReport("Calculating " + rc["originForeignKey"] + " field values ...")
        edit.CalculateField_management(rc["destinationTable"], rc["originForeignKey"], 
            "!" + rc["realForeignKey"] + "!", "PYTHON")
0 Kudos
Roxana_ElenaUrdea
New Contributor III

I also have the same problem when trying to execute an update cursor on a network dataset feature...

Have you tried to open the editing session like this:

"with arcpy.da.Editor(workspace) as edit:

     ...

"

0 Kudos
JordanParfitt
Occasional Contributor

I did.... I ended up just versioning the data although you shouldn't have t.

This didnt work either:

edit.startEditing(False, False)

0 Kudos
DanPatterson_Retired
MVP Emeritus

from a related thread for geometric networks

NIM - 102778 error and workaround

worth a try

0 Kudos