3

How can I alter the aliases of the required fields (ObjectID, Shape, Shape_Length, Shape_Area) in a GDB with Python?

For example I have this code:

rf = ur'D:\Δοκιμές\Tester'
gdb = rf+'\\Ant.gdb'
p = ['Parcels','Parts']
for a in range(2):
    RFields = OrderedDict([(arcpy.Describe(gdb+'\\'+p[a]).OIDFieldName,u'Α/Α'),(u'Shape',u'Γεωμετρία'),(arcpy.Describe(gdb+'\\'+p[a]).lengthFieldName,u'Περίμετρος'),(arcpy.Describe(gdb+'\\'+p[a]).AreaFieldName,u'Εμβαδόν')])
    for key in RFields.keys():
        arcpy.AlterField_management(gdb+'\\'+p[a],key,key,RFields[key])

But it produces an error:

Failed to execute (AlterField).

Is it possible to do what I want?


The full error:

Runtime error  Traceback (most recent call last):   
  File "<string>", line 8, in <module>   
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 3239, in AlterField     
raise e ExecuteError: 
  ERROR 001600: Failed to alter field name. 
  ERROR 001660: Cannot alter field name on required fields. 
Failed to execute (AlterField).
2
  • 1
    Does it give an error number with that "Failed to execute"?
    – Midavalo
    Commented Mar 24, 2017 at 14:27
  • Ir gives the following error: Runtime error Traceback (most recent call last): File "<string>", line 8, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 3239, in AlterField raise e ExecuteError: ERROR 001600: Failed to alter field name. ERROR 001660: Cannot alter field name on required fields. Failed to execute (AlterField).
    – Panos
    Commented Mar 25, 2017 at 12:58

2 Answers 2

2

Have you tried altering these aliases using the ArcToolbox tool directly?

The error you give says "Failed to alter field name" - this is because you are specifying a New Field Name value, even though it is the same as the existing name the tool is trying to update the field name which it can't in a system field. You can bypass this error by removing the new field name from your tool:

arcpy.AlterField_management(gdb+'\\'+p[a], key, "", RFields[key])
#                                               ^^ new field name removed

However, it still will not work as you will hit other errors that cannot be worked around.

It appears that you are unable to use the Alter Field tool to modify the ArcGIS managed system fields. This inability to alter the field alias here may be unintended, and could be worth reporting to Esri.

To test, here I am using the tool to try to update the alias on my SHAPE_Length field - note that Field Name, Type, Length, Nullable are all disabled in the tool so I cannot modify them:

enter image description here

And the error raised:

enter image description here

I tried the same on the SHAPE field using arcpy, explicitly setting the field_is_nullable to NON_NULLABLE but again the same error.

>>> arcpy.AlterField_management("testPoly", "SHAPE", new_field_alias="NewShape", field_is_nullable='NON_NULLABLE')

Runtime error  Traceback (most recent call last):   
  File "<string>", line 1, in <module>   
  File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\management.py", line 3537, in AlterField     
raise e ExecuteError: 
  ERROR 001625: Failed to alter field is nullable. Failed to execute (AlterField). 

Note that I found running Alter Field on the OBJECTID field did not give any errors, but also didn't modify the alias for this field.

I would suggest you may need to manually change each alias within the Attribute Table, as the tool doesn't appear to allow it for these ArcGIS system controlled fields.

4
  • I tried to use the tool directly from ArcToolbox with ObjectID and it produces no error. The problem is that it changes the alias in database and I could check it from ArcCatalog but when I open the table in Arcmap the Alias looks unchanged. This is a really strange behavior.
    – Panos
    Commented Mar 24, 2017 at 22:18
  • @Panos it won't update it. Test it from the tool on other fields - mine also gave no error on Object ID but still didn't work. It gave error on all the other fields
    – Midavalo
    Commented Mar 24, 2017 at 22:26
  • @Panos I have updated the beginning of my answer in response to your full error message.
    – Midavalo
    Commented Mar 25, 2017 at 23:35
  • @Panos, a quick note for you - updating a feature class field alias doesn't update the field alias in a map layer that refers to this feature class. As soon as you've added a feature class on a map, it is no longer aware of the its feature class fields aliases. Commented Mar 26, 2017 at 7:36
1

It seems as you are hitting the limits of the GP tool Alter Field. I've also tried to reproduce the error you are getting (I am on 10.4.1) and run into the same issues - updating the map layer field aliases in ArcMap Layer Properties and in Feature Class Properties windows works fine. However, running the GP tool returns an error.

The workaround is to call ArcObjects that will modify the feature class fields aliases. It is also possible to edit aliases only of map layers without modifying the underlying feature class fields aliases.

If you wouldn't want to update this information manually, you could call ArcObjects using C#/VB, Java, or C++. As you are using Python, I've written a module for this. Look at Accessing ArcObjects from Python to learn more about how you can get started.

## -*- coding: UTF-8 -*-

import os
from collections import defaultdict
from comtypes.client import GetModule, CreateObject
from snippets102 import GetStandaloneModules, InitStandalone

GetStandaloneModules()
InitStandalone()

esriCarto = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriCarto.olb")
esriGeodatabase = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriGeoDatabase.olb")
esriDataSourcesGDB = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriDataSourcesGDB.olb")

file_gdb_pointer = CreateObject(progid=esriDataSourcesGDB.FileGDBWorkspaceFactory,
                                interface=esriGeodatabase.IWorkspaceFactory)

#0 is The hWnd argument is the parent window or application's window. The hWnd will
#guarantee that the connection dialog, if presented to you because of insufficient
#properties, has the correct parent.
file_gdb = file_gdb_pointer.OpenFromFile(r'C:\GIS\Temp\ArcGISHomeFolder\Default.gdb',hWnd=0)

#access contents inside gdb
feature_workspace = file_gdb.QueryInterface(esriGeodatabase.IFeatureWorkspace)

#open feature class inside gdb
fc = feature_workspace.OpenFeatureClass('Buffer1km')

#edit field alias
schema = fc.QueryInterface(interface=esriGeodatabase.IClassSchemaEdit3)
for k, v in {'Shape': u'Геометрия',
             'Shape_Length': u'Периметр',
             'Shape_Area': u'Площадь'}.iteritems():
    schema.AlterFieldAliasName(k, v)
1
  • I will check it out. It looks interested!
    – Panos
    Commented Mar 26, 2017 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.