6 Replies Latest reply: Jun 21, 2012 9:45 PM by nahmed-esristaff

    Inconsistencies in Field type enumerations are stupid

    kimo
      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',..}
        • Re: Inconsistencies in Field type enumerations are stupid
          foresterguy
          I feel your frustration and share your 'angry face'
          • Re: Inconsistencies in Field type enumerations are stupid
            Stringtheory
            I agree.  I've had to write a bunch of extra code to test for and set the proper item definitions, it's annoying.
            • Re: Inconsistencies in Field type enumerations are stupid
              ruckc
              Just another example of ESRI doing the minimum possible, never taking that extra step.
              • Re: Inconsistencies in Field type enumerations are stupid
                Stringtheory
                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
                • Re: Inconsistencies in Field type enumerations are stupid
                  nahmed-esristaff
                  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.