Versioned data and when edit session must be used for updatecursor

2260
8
07-17-2020 08:27 PM
LauraTateosian
New Contributor III

Hi Folks,

The updatecursor help page says:   

"Using UpdateCursor on versioned data requires the start of an edit session" 

--What does "versioned data" mean?

I found that I needed to use an edit session when I was trying to use the updatecursor on the attribute table of a feature class in a file geodatabase. I created the feature class using the geotagged photos to points tool. I was not using an insert cursor simultaneously, so I assume (based on the tips in this help page) that this means my data is versioned.

Is data in a file geodatabase versioned by default? Is this a setting I can toggle?

It took me a long time to determine how to resolve the error, as I did not have to use the Editor object to perform the same operations on a dataset that was created in a similar manner. So I'm trying to figure out what triggered this requirement.

Thanks in advance for your help!

0 Kudos
8 Replies
DavidPike
MVP Frequent Contributor

Hi Laura,

Your data is not versioned if it is in a FGDB.  

I wish I didn't know what versioning means if I'm honest, be careful what you wish for.

Was your script running in immediate mode (the python window)?  I've not seen the editing requirement before, could you share your code?

0 Kudos
LauraTateosian
New Contributor III

Hi David, 

thanks for the reply.  Can you (or others) point me to the ESRI definition of versioned data so that I may know in which situations (outside of file geodatabases) the editor needs to be used? 

So, this means there's something else about this data that is causing the problem.

To answer your questions:

1) I tried the code in PyCharm (Python 3.7), PythonWin (Python2.7), and the ArcGIS Pro Python window and consistently receive the same RuntimeError message when it hits the for loop.  

2) Here's some simple code that throws an error on line 5

import arcpy

imgPoints = r"C:\theDatabase.gdb\theFC"
uc = arcpy.da.UpdateCursor(imgPoints, "*")
for entry in uc:
    print(entry)
del uc‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Error:  Unhandled exception while debugging...
Traceback (most recent call last):
File "C:\theScript.py", line 5, in <module>
for entry in uc:
RuntimeError: Objects in this class cannot be updated outside an edit session 

In the following code, I've added a line that creates the edit session (line 4). With this edit, the code completes without error:

import arcpy

imgPoints = r"C:\theDatabase.gdb\theFC"
with arcpy.da.Editor(r"C:\theDatabase.gdb") as edit:
    uc = arcpy.da.UpdateCursor(imgPoints, "*")
    for entry in uc:
        print(entry)
    del uc‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I realize an update cursor is not needed to simply read the data.  In my real code, I was editing the data.  But I find it interesting that the error occurs when you simply try to iterate through the records, not when you're trying to commit a change.   

0 Kudos
DavidPike
MVP Frequent Contributor

Versioning 101 

Are you completely sure this is a standalone FGDB that sits on your local drive? There's no connection file such as .sde in the path?

To me it seems you are trying to run the UpdateCursor on a versioned Enterprise GDB FC.

0 Kudos
LauraTateosian
New Contributor III

Hi David,

thank you for following up.  There is no SDE or Enterprise GDB involved.  It's a File Geodatabase that I created by right-clicking in the Catalog pane of Pro and selecting New > File Geodatabase 

I thought you might be onto something because my file geodatabase is on Google File Drive, but I tried copying the file geodatabase to my local drive and got the same behavior.

0 Kudos
DavidPike
MVP Frequent Contributor

Is this the only line of code you're running? Theres something in the help about multiple curators running in the same workspace.

0 Kudos
LauraTateosian
New Contributor III

Hi David,   yes, it's just the one script trying to edit the table.

0 Kudos
DavidPike
MVP Frequent Contributor

Looking at your initial post, I would say your photo points are in a relationship class with the photos.  The edit session is a requirement as it enables both to be updated with changes.

LauraTateosian
New Contributor III

Hi David, yes, that appears to be true. I tested the same simple procedure (update cursor with no edit session) on a feature class that was not "in a relationship class", but was in the same file geodatabase and no error was thrown.  This makes me think that the help page for the update cursor needs to list at least one additional situation in which an edit session is required (unless being in a relationship means it's versioned?). 

Currently, the help page says this:    

Note:

Opening simultaneous insert or update operations on the same workspace using different cursors requires the start of an edit session.

Note:

Using UpdateCursor on versioned data requires the start of an edit session.

There is no mention of datasets in a relationship needing an edit session.  I wonder if there will be additional surprise situations in which an edit session is required.  

Take-home lessons from this, dear readers:

  1. When you're trying to update an attribute table and you see this error:

    RuntimeError: Objects in this class cannot be updated outside an edit session

    pop in a line of code like this:
with arcpy.da.Editor(workspacePath) as edit:

      to wrap around your update cursor code.  And don't ask why. 

   2.  If you create a feature class using the GeoTagged Photos To Points (Data Management) tool, you will need an edit session to make updates to its attribute table with an update cursor.

   3.  Don't worry about having the data on Google File Drive.  That should be fine.