5

I'm trying to update a table using a python script and it's not working. The table resides in a postgres geodatabase and is versioned. I have also verified my user permissions (I'm the admin). The version is public (i.e. not protected). The table properties show that the current user has select, insert, update, and delete privileges.

Here's my command

with arcpy.da.Editor(arcpy.env.workspace) as edit: 
    with arcpy.da.InsertCursor("service", ["service_id"]) as cur:
        cur.insertRow([0])

The error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 3, in <module>
RuntimeError: The requested operation is invalid on a closed state [ugdb.sde.service]
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
ShaunLangley
  • 776
  • 5
  • 16
  • I have to admit, that I haven't used the Editor Class so far, but just an idea: Have you tried to call edit.startEditing(True, True) and/or edit.startOperation() explicitely before using an Insert cursor? Does one of these fail? Have you tried the same on the Python Shell? – Jürgen Zornig Dec 10 '13 at 18:31
  • I have and that does work.... finally (I'm not sure what changed). I was going of instructions at http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000005000000. – ShaunLangley Dec 10 '13 at 18:37
  • I have problems with arc running in the background and not quitting. I thought this might fix the problem and figured finer control over the editing might help. – ShaunLangley Dec 10 '13 at 18:38
  • According to WebHelps Code Sample 2, I assume Insert Cursors possibly only work without pythons context manager ('with')...ESRI logic :p ... I see myself often confronted with these type of problems with arcpy. If it works without context manager, I would ask ESRI themselves (your distributor) if there are any differences, and why. – Jürgen Zornig Dec 10 '13 at 18:47

1 Answers1

6

After much exploration, I finally found the solution. It has to do with how your geodatabase is configured and this is not explained in the documentation.

If the tables are versioned with the option to move the edits to the base, THEN you do not use startOperation() and stopOperation(). HOWEVER, if your tables are versioned in the default manner, then you need to control transaction operations explicitly. I can't say I fully understand why this makes a difference, but in my case it was absolutely the solution.

ShaunLangley
  • 776
  • 5
  • 16
  • Thank you for posting this...This is definitely something ESRI needs to put in their documentation. – crmackey May 15 '15 at 14:56