Bug
| Bug ID Number | BUG-000156786 |
|---|---|
| Submitted | March 21, 2023 |
| Last Modified | April 29, 2025 |
| Applies to | ArcGIS Pro |
| Version found | 3.1 |
| Operating System | Windows OS |
| Operating System Version | N/A |
| Version Fixed | 3.3 |
| Status | As Designed |
In the user-provided code sample to reproduce the issue, the fieldsMappings.addTable(layer) object is creating the field mapping with all of the input fields, which is backwards; the target feature class table must first be added to the fieldMappings object before the input, so that the input fields automatically matches to the field names from the target when the names are the same. This matches the Append tool behavior in the Geoprocessing pane, where the field map loads all of the fields from the target, and users can map source fields from the input to those target fields. The code below works for the reproduced case provided for ArcGIS Pro 3.1:
## Start sample code
import sys, os, arcpy
input = os.path.join(arcpy.env.scriptWorkspace,'gpsdata.gdb\\points_source')
target = os.path.join(arcpy.env.scriptWorkspace,'gpsdata.gdb\\points_destination')
fieldMappings = arcpy.FieldMappings()
# Add all fields from target layer
fieldMappings.addTable(target)
# Add all fields from input layer, which will automatically match to field names from the target when the names are the same
fieldMappings.addTable(input)
arcpy.AddMessage(f"initial fieldmap = {fieldMappings.exportToString()}")
# Loop through all fields in the field map, and clean out the input fields from the target, so each field map only has an input from the input layer, which matches the behavior of the Append tool UI
for fmi in range(fieldMappings.fieldCount):
fm = fieldMappings.getFieldMap(fmi)
for targetfield_index in range(fm.inputFieldCount):
intable = fm.getInputTableName(targetfield_index)
if intable == arcpy.Describe(target).catalogPath:
fm.removeInputField(targetfield_index)
fieldMappings.replaceFieldMap(fmi, fm)
break
# find the existing guid field in the target layer
guid_field_index = fieldMappings.findFieldMapIndex("GUIDCopy")
guid_fieldmap = fieldMappings.getFieldMap(guid_field_index)
# add the input globalID field to the guid fieldmap
guid_fieldmap.addInputField(input, "GlobalID")
fieldMappings.replaceFieldMap(guid_field_index, guid_fieldmap)
arcpy.AddMessage(f"final fieldmap = {fieldMappings.exportToString()}")
arcpy.Append_management(input, target, "NO_TEST", fieldMappings)
## End sample code
Bug ID: BUG-000156786
Software:
Get help from ArcGIS experts
Download the Esri Support App