arcpy.Append_management from FGDB to SDE

2795
5
11-17-2016 11:59 AM
Highlighted
Occasional Contributor III

Hello everyone,

I need to append a polygon layer located in a FGDB to the same layer located in SDE.

arcpy.Append_management(FGDB__Layer, SDE_Layer , "TEST", "", "")  fails because the structure is not the same. Polygon layers in FGDB have 2 additional fields SHAPE.AREA and SHAPE.LENGTH

I need to use "NO_TEST" on the schema_type

arcpy.Append_management(FGDB__Layer, SDE_Layer , "NO_TEST", ??????, "")

How do I create the field_mapping list? 

Thanks

5 Replies
Highlighted
MVP Regular Contributor
Highlighted
MVP Regular Contributor

You can figure it out if you spend enough time in the Help Docs but it definitely takes a different way of thinking. Mitch Holley‌ has some good links too.

I was in a similar situation and created some code that would create the field mappings based solely on the field name. Any field names that don't match will be left out.

import arcpy

def main():
    CMGENINV = r"C:\temp\some_geodatabase.gdb\CMGENINV"
    TX_LUCITY_CASE_DATA = r"C:\GISConnections\SDE@GTEST.sde\TX.LUCITY_CASE_DATA"

    try:
        fieldmappings = fuzzy_fieldmap(CMGENINV, TX_LUCITY_CASE_DATA)

        arcpy.Append_management(
            CMGENINV,
            TX_LUCITY_CASE_DATA,
            "NO_TEST",
            fieldmappings
        )
        print arcpy.GetMessages()
    finally:
        # Cleanup
        arcpy.ClearWorkspaceCache_management()


def fuzzy_fieldmap(input_table, target_table):
    input_fields = [
        f.name.upper() for f in arcpy.ListFields(input_table)
        if f.type != "OID" or f.name != "OBJECTID"
    ]

    target_fields = [
        f.name.upper() for f in arcpy.ListFields(target_table)
        if f.type != "OID" or f.name != "OBJECTID"
    ]

    fms = arcpy.FieldMappings()  ## Main FieldMapings object to hold FieldMap objects
    fm_vars = {}  ## dictionary for FieldMap objects

    for t_field in target_fields:
        if t_field in input_fields:
            # Create the FieldMap object
            fm_vars[t_field] = arcpy.FieldMap()
            # Add fields to FieldMap object
            ## Add target field first so the output gets those field properties
            fm_vars[t_field].addInputField(target_table, t_field)
            fm_vars[t_field].addInputField(input_table, t_field)
            # Add the FieldMap objects to the FieldMappings object
            fms.addFieldMap(fm_vars[t_field])

    # Optional debugging section to print field mappings
    for out_field in fms.fields:
        print "{} ({}): {}".format(out_field.name, out_field.aliasName, out_field.type)

    return fms


if __name__ == '__main__':
    main()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Highlighted
Occasional Contributor III

Hi, Theres some stuff about field maps here also that may help you!

https://community.esri.com/thread/185431-append-tool-and-field-mapping-help-examples 

Highlighted
Occasional Contributor III

This is what  I did and it works:

fieldmappings = arcpy.FieldMappings()
 fieldmappings.addTable(FGDBLayer)
 fieldmappings.addTable(SDELayer)

 arcpy.Append_management(FGDBLayer, SDELayer, "NO_TEST", fieldmappings, "")

Highlighted
New Contributor III

Jose you are a genious!

This is the easiest solution if the fieldnames are the same in both tables!

Reply
0 Kudos