1

Arcpy AddIndex_management won't index long text fields, is there a workaround?

I tried:

arcpy.AddIndex_management(r'C:\workspace\foo.dbf', "Text")

and got the error:

Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 5993, in AddIndex     
  raise e ExecuteError: 
  ERROR 999999: Error executing function. 
  Attribute indexes cannot be created for fields with a length that is greater than 80. 
Failed to execute (AddIndex).

I have also tried this with shapefiles as well as dbf's with the same error. I need to index a text field in order to perform a JoinField() more quickly with arcpy.

Is there a work around to do this that doesn't involve shortening text fields?

I haven't seen a reason not to index fields longer than 80 characters; especially since the text fields I'm indexing have been calculated and are therefore not very prone to typos. For example, if you had a series of field of species names and you wanted to join that to a table containing descriptions of those species, you would be joining on a potentially long text string.

  • The problem here appears to be the length of the text field, rather than the fact that it's a text field. Have you tried it on fields shorter than 80 characters? Performing a join on a value longer than 80 characters seems unusual and risky (for typos) to me. – Midavalo Apr 5 '17 at 15:17
  • I have now and it does work. I haven't seen a reason not to index fields longer than 80 characters; especially since the text fields I'm indexing have been calculated and are therefore not very prone to typos. For example, if you had a series of field of species names and you wanted to join that to a table containing descriptions of those species, you would be joining on a potentially long text string. – Craig T Apr 5 '17 at 15:36
  • Could you create a new coded value field for those values in each table, and index and join on those? – Midavalo Apr 5 '17 at 15:41
  • I suppose I could. Ok, next question is if I am exporting an excel file with a text field to a dbf, how do I get the length of that field to not be the standard 256? Should I ask that as another question? – Craig T Apr 5 '17 at 15:49
  • Yes ask as a separate question – Midavalo Apr 5 '17 at 15:59
1

If you CAN edit the length of text fields the answer is as follows:

arcpy.AddField(r'C:\workspace\foo.dbf', "shortText", "#", "#", 75, "#", "#", "#", "#")
with arcpy.da.updateCursor(r'C:\workspace\foo.dbf', ["Text", "shortText"]) as cursor:
    for row in cursor:
        row[1] = row[0]
        cursor.updateCursor(row)
arcpy.AddIndex_management(r'C:\workspace\foo.dbf', "shortText")

and then perform your join on "shortText"

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.