2

The following snippet works perfectly when appending one FC to another, with a slight schema difference (1 field):

target_layer = "blah blah blah.sde/yada.DBO.yada_yada"
append_layer = renamefgdb+'\some_fc'
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(target_layer)
fieldmappings.addTable(append_layer)
field_to_map_index = fieldmappings.findFieldMapIndex("globalid")
field_to_map = fieldmappings.getFieldMap(field_to_map_index)
field_to_map.addInputField(append_layer, "PIC_ID")
fieldmappings.replaceFieldMap(field_to_map_index, field_to_map)
arcpy.Append_management(append_layer, target_layer, "NO_TEST", fieldmappings, "")

But I need for the objectid in the append layer to also migrate to the target layer, unchanged. Not sure if this is possible?

When I try

target_layer = "C:/blah blah blah.sde/blah.DBO.blah"
append_layer = renamefgdb+'\yada_yada_yada'
fieldmappings = arcpy.FieldMappings()

fieldmappings.addTable(target_layer)
fieldmappings.addTable(append_layer)
list_of_fields_we_will_map = []
list_of_fields_we_will_map.append(('PIC_ID', 'globalid'))
list_of_fields_we_will_map.append(('objectid', 'objectid'))
for field_map in list_of_fields_we_will_map:
field_to_map_index = fieldmappings.findFieldMapIndex(field_map[0]) 
field_to_map = fieldmappings.getFieldMap(field_to_map_index)
field_to_map.addInputField(append_layer, field_map[1])
fieldmappings.replaceFieldMap(field_to_map_index, field_to_map)

arcpy.Append_management(append_layer, target_layer, "NO_TEST", fieldmappings, "")

it fails with

Traceback (most recent call last):
 File "C:/ARCHIVE_AA_S123_TEST.py", line 67, in <module>
 field_to_map = fieldmappings.getFieldMap(field_to_map_index)
 File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\arcobjects.py", line 328, in getFieldMap
 return convertArcObjectToPythonObject(self._arc_object.GetFieldMap(*gp_fixargs(args)))
RuntimeError: FieldMappings: Error in getting field map from field mapping for GetFieldMap

and this fails with the same error:

target_layer = "C:/blah blah blah.sde/blah.DBO.blah"
append_layer = renamefgdb+'\yada_yada_yada'
fieldmappings.addTable(target_layer)
fieldmappings.addTable(append_layer)
field_to_map_index = fieldmappings.findFieldMapIndex("globalid")
field_to_map_index_2 = fieldmappings.findFieldMapIndex("objectid")
field_to_map = fieldmappings.getFieldMap(field_to_map_index)
field_to_map_2 = fieldmappings.getFieldMap(field_to_map_index_2)
field_to_map.addInputField(append_layer, "PIC_ID")
field_to_map_2.addInputField(append_layer, "objectid")   
arcpy.Append_management(append_layer, target_layer, "NO_TEST", fieldmappings, "")

Is this not working because it's simply not possible to append the objectid? Unfortunately, this project requires that the objectid be maintained when moving the data.

5

Is this not working because it's simply not possible to append the objectid?

Correct - the OBJECTID field is maintained by ArcGIS system, you cannot modify it yourself, and you can't force them to remain the same when copying the features to another feature class. The ObjectID must remain Unique and non-null - being able to keep ObectIDs would make it possible to have duplicates (If an objectID already existed, and you copied another feature in with the same ObjectID).

Unfortunately, this project requires that the objectid be maintained when moving the data.

This is not possible with the OBJECTID field itself. Create a new ID field and use that instead of ObjectID. You could copy your existing ObjectID values into this field to begin with, and then just update this field as required. These would then remain when copying/appending to another feature class.

See What is an ObjectID? and Fundamentals of ObjectID fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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