[GIS] A column was specified that does not exist

arcgis-collectorpython

I am using ArcGIS Desktop 10.4.1 and ArcGIS Collector.

I've tried to follow the answer given to my last question here about naming attachments from Collector. When I run the script, I get the error message:

Traceback (most recent call last):
  File "D:\extract.py", line 20, in <module>
    for row in cursor:
RuntimeError: A column was specified that does not exist

I see from other users that have this problem that the problem can be related to the naming of the column (that I haven't named it correctly), but I can't find out the right way.

This is my script as it stands now:

import arcpy, os
from collections import defaultdict


inFC = r'D:\Drammen.gdb\Drammen' # Feature Class 
inTable = r'D:\Drammen.gdb\Drammen__ATTACH' # Attachment table
fileLocation = r'D:\Test' # Output location 

# Get dictionary of ObjectID and associated field value
myFeatures = dict()
with arcpy.da.SearchCursor(inFC, ['OID@', 'Sefrakid']) as cursor:
    for row in cursor:
        myFeatures[row[0]] = row[1]

# Create dictionary to count usage of the field value (to increment files)
valueUsage = defaultdict(int)

# Loop through attachments, incrementing field value usage, and using that
# increment value in the filename
with arcpy.da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'REL_OBJECTID']) as cursor:
    for row in cursor:
        if row[3] in myFeatures:
            attachment = row[0]
            fieldValue = myFeatures[row[3]] # Value of specified field in feature class
            valueUsage[fieldValue] += 1 # Increment value
            filename = "{0}_{1}".format(fieldValue, valueUsage[fieldValue]) # filename = FieldValue_1
            output = os.path.join(fileLocation, filename) # Create output filepath
            open(output, 'wb').write(attachment.tobytes()) # Output attachment to file

And this is the table from the FC (I want the column Sefrakid to be the name for the jpgs):
enter image description here

And this is how the ATTACH table looks:

enter image description here

I do notice that compared to Midavalos answer in my recent post, I do have some REL_GLOBALID which he doesn't. Maybe that has something to do with it?

I am (as you now know) very inexperienced with Python (or programming in general), so it's not easy for me to see obvious errors in the script.

Can anyone point me to where the mistake is?

Best Answer

I'm afraid that you've fallen victim to the Mystery Attachment Table Schema. The format that ArcGIS "usually" uses for __ATTACH files is this:

enter image description here

However, under certain unknown conditions, the __ATTACH file format that is created by the Add Attachments tool comes out like this:

enter image description here

Technicians who experience the mystery __ATTACH file format swear that they've created the file through the Add Attachments tool like they've always done. It may be a case of differing ArcGIS Desktop versions, but I've not investigated that.

The really weird thing is that the second __ATTACH file format is actually much better to use. Relationships built using a feature class objectid are eventually bound for a bad breakup. :-)