AnsweredAssumed Answered

Cannot add/remove attachments in python when table/featureclass is versioned

Question asked by mleahyesri-ca-esridist Employee on Dec 7, 2012
Latest reply on Dec 17, 2012 by JPenney-esristaff
Hello all,

Has anyone run into problems when using the arcpy.AddAttachments_management() function with tables that have been registered as versioned? I can't seem to successfully do it...even though attachments work fine through other means (e.g., via a REST interface for a FeatureServer).

In my specific case, I have an enterprise sde geodatabase (using PostgreSQL). Before anything gets registered as versioned, I can add attachments as usual. Once things are versioned, however, it's a different story. Here's the easiest way for me to summarize this:

 # Works arcpy.AddAttachments_management("C:/test/test.sde/test.sde.test_table","objectid","C:/test/test.csv","FileID","FilePath","#")  # Cleanup arcpy.RemoveAttachments_management("C:/test/test.sde/test.sde.test_table","objectid","C:/test/test.csv","FileID","#")  # Register the table as versioned: arcpy.RegisterAsVersioned_management("C:/test/test.sde/test.sde.test_table")  # Produces this error: arcpy.AddAttachments_management("C:/test/test.sde/test.sde.test_table","objectid","C:/test/test.csv","FileID","FilePath","#")  # Unregister the table as versioned: arcpy.UnregisterAsVersioned_management("C:/test/test.sde/test.sde.test_table")  # Still produces this error: arcpy.AddAttachments_management("C:/test/test.sde/test.sde.test_table","objectid","C:/test/test.csv","FileID","FilePath","#")  # Oops, looks like the __ATTACH table is still versioned (gets registered automatically, but not unregistered) # Note: if I only unregister the __ATTACH table, then the table it is associated with does get automatically unversioned. arcpy.UnregisterAsVersioned_management("C:/test/test.sde/test.sde.test_table__ATTACH")  # Now this works again: arcpy.AddAttachments_management("C:/test/test.sde/test.sde.test_table","objectid","C:/test/test.csv","FileID","FilePath","#") 

If I have the same table/featureclass accessible through an editable feature service, the addattachments REST calls work fine, and respect versioning. This problem seems to be limited to only python. As a workaround, I guess I can manually insert records into the __ATTACH this a bad idea? This seems to work okay:

 edit = arcpy.da.Editor("C:/test/test.sde") edit.startEditing(False, True) edit.startOperation()  cur = arcpy.da.InsertCursor("C:/test/test.sde/test.sde.test_table__ATTACH",["rel_objectid","content_type","att_name","data_size","data"]) file = memoryview(open("C:/test/test.pdf","rb").read()) row = [801,"application/pdf","test.pdf",len(file.tobytes()),file] cur.insertRow(row)  edit.stopOperation() edit.stopEditing(True) 

After performing the above, everything else (e.g., a FeatureServer) seems to recognize the attachments without any noticeable problem. Is it a bad idea to do this as a workaround? My interpretation is that all the add/remove features tools do is modify the contents of the __ATTACH tables, so it seems like inserting rows like the above should be safe to do.

Any thoughts/suggestions?