I am creating a script that makes a feature class using an existing on SDE, puts some data in it, and runs some processing on it, and then appends the data back to the original feature class using the append tool.
I create a tmp feature class in memory for this, which seems fine, but when I go to append back to the original feature class, the field names are different.
For example, OBJECTID field on SDE is renamed objectid in memory, thus breaking the append (similar behavior for any st_* field).
Is there a solution to this? I'd rather not use insert and update cursor...
What functions do you use to create the feature class and do the append? Some thoughts:
col_dict = {
"oldname_1": "NEWNAME1",
"oldname_2": "NEWNAME2"
}
temp_layer.rename(columns=col_dict, inplace=True)
Something that might be easier is using an Insert Cursor to avoid stuff like that. Shape@, OID@ are both very useful.
That being said, if you're appending stuff in, I don't think the OBJECTID matters? Or if it does, you should probably have it as a separate field that you manage.
The other question is: Why not just do everything in the original feature class, just using a version to do it? You have the enterprise gdb anyway; you may as well take advantage of it.
I think the issue is that this is essentially an UPSERT operation, where not all the old content is updated. So I want to keep un-updated old records, update some existing ones, and append new ones (if they exist).
I'm using insert/create cursors, but hoping to make this more efficient.
Try running the Append with the 'NO_TEST' option for the schema_type parameter.
Maybe use field mapping,
import arcpy
# Paths to your original feature class (SDE) and in-memory feature class
sde_fc = "feature_class"
in_memory_fc = "in_memory/tmp_feature_class"
# Create FieldMappings object
field_mappings = arcpy.FieldMappings()
# Add fields from the in-memory feature class to the FieldMappings
for field in arcpy.ListFields(in_memory_fc):
field_map = arcpy.FieldMap()
field_map.addInputField(in_memory_fc, field.name)
# If needed, set the output field name to match the SDE feature class field
if field.name.lower() == "objectid":
output_field = field_map.outputField
output_field.name = "OBJECTID"
field_map.outputField = output_field
field_mappings.addFieldMap(field_map)
# Append with FieldMappings to ensure they are correct
arcpy.management.Append(in_memory_fc, sde_fc, "NO_TEST", field_mappings)
Also, you might try to disable qualified field names.
# Disable qualified field names
arcpy.env.qualifiedFieldNames = False
# Perform Append
arcpy.management.Append(in_memory_fc, sde_fc, "NO_TEST")