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]
  • 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 resources.arcgis.com/en/help/main/10.2/index.html#//…. – 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
5

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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