Inconsistencies in Field type enumerations are stupid

2251
6
05-02-2010 09:28 PM
KimOllivier
Occasional Contributor III
When I do a Describe on a field in a featureclass to be able to create a foreign key in a new table you would think that the terms would match those required in AddField but they don't and the alternatives are not accepted in the tool.

'SmallInteger' has to change to 'SHORT' and so on.
The only way is to make up a special dictionary as a workaround

eg dFixTypeHack = {'SmallInteger": 'SHORT',..}
0 Kudos
6 Replies
JeffChristiansen
New Contributor
I feel your frustration and share your 'angry face'
0 Kudos
JoelCalhoun
New Contributor III
I agree.  I've had to write a bunch of extra code to test for and set the proper item definitions, it's annoying.
0 Kudos
CurtisRuck
New Contributor III
Just another example of ESRI doing the minimum possible, never taking that extra step.
0 Kudos
JoelCalhoun
New Contributor III
I ended doing something like this.   Maybe a special dictionary would be a better way to go?

def Rename_Field(fc_path, field_name):
    field_name_orig = field_name + "_orig"
    gp.addmessage("Renaming field: " + field_name + " to " + field_name + "_orig")
    print "Renaming field: " + field_name + " to " + field_name_orig
    field_list = gp.ListFields(fc_path, field_name)
    field = field_list.next()
    type = str(field.Type)
    length = str(field.Length)
    
    # Case handling; Returned type of "String" uses the a type of "text" and also uses length as an AddField parameter. 
    if (type == "String"):
        type = "TEXT"
        gp.AddField_management(fc_path, field_name_orig, type, "", "", length, "", "NULLABLE", "NON_REQUIRED", "")
        gp.CalculateField(fc_path,field_name_orig, "[" + field_name + "]")
        gp.DeleteField_management(fc_path, field_name)
    
    else :
        # Case handling; Returned type of "Integer" converted to a type of "long" for use as AddField parameter.
        if (type == "Integer"):
            type = "LONG"
            gp.AddField_management(fc_path, field_name_orig, type, "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
            gp.CalculateField(fc_path,field_name_orig, "[" + field_name + "]")
            gp.DeleteField_management(fc_path, field_name)
            
        # Case handling; Returned type of "SmallInteger" converted to a type of "short" for use as AddField parameter.
        elif (type == "SmallInteger"):
            type = "SHORT"
            gp.AddField_management(fc_path, field_name_orig, type, "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
            gp.CalculateField(fc_path,field_name_orig, "[" + field_name + "]")
            gp.DeleteField_management(fc_path, field_name)
            
        # Case handling; Returned type other than "String", "Integer", or "SmallInteger" handled normally.
        else :
            gp.AddField_management(fc_path, field_name_orig, type, "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
            gp.CalculateField(fc_path,field_name_orig, "[" + field_name + "]")
            gp.DeleteField_management(fc_path, field_name)           

    # Reset variables
    name = ""
    type = ""
    length = ""
    fc_path = ""
    field_name = ""



Joel
0 Kudos
GusMartinka
Occasional Contributor
Not impressed.
0 Kudos
NobbirAhmed
Esri Regular Contributor
If you have ArcGIS 10.0 or 10.1 then you don't need to worry about keeping a dictionary. I'm on 10.0 and this code works:

dsc = arcpy.Describe(infc)

fields = dsc.fields

for field in fields:
    print field.name, field.type
    arcpy.AddField_management(infc2, field.name, field.type)


Although the print statement shows the field type to be SmallInteger - internally it is made consistent with Short type.
0 Kudos