Monday 15 April 2019

python - Why does arcpy.ListFields list GUID field as SmallInteger?


I want to get a list of all GUID fields in a feature class, but it seems that arcpy.ListFields is listing my GUID field as a SmallInteger. What am I doing wrong?


global_id_fieldnames = [f.name for f in arcpy.ListFields(layer) if f.type.lower() == 'guid']


after this list was empty, I called arcpy.AddMessage() for each of the fields:


for f in arcpy.ListFields(layer):
arcpy.AddMessage('{},{}'.format(f.name, f.type))

enter image description here



Answer



I emailed esri support and got this response:



There is a known bug (#NIM055662) for this behavior: [#NIM055662 ListFields and Describe methods are returning SmallInteger FieldType for GUID fields.]


The work-around is to check the field length; if it is longer than a standard "small integer" length, it's a GUID. Field type for a GlobalID field is SmallInteger with length 38 in my test case, much longer than the number of digits in a small integer. A small integer length may vary depending on the platform (although probably not on a Windows platform), but it would not be as large as 38.




following esri's advice, I'm now using:


global_ids_fields = [f.name for f in arcpy.ListFields(layer) if f.type.lower() == 'smallinteger' and f.length == 38]

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...