Append tool not honoring common fields

430
4
Jump to solution
02-07-2019 12:26 PM
MKF62
by
Occasional Contributor III

I am using a FGDB feature class that has a field called "MgmtTractID" of string type. It has the exact same properties and name as a field in my feature class in an SDE geodatabase. Unfortunately, the attributes from MgmtTractID in my FGDB will not transfer to the SDE MgmtTractID field. I'm not sure why, if I use the Append tool (with selections on my FGDB feature class) in the GUI, it works fine. I use "NO_TEST" because some of the fields aren't common between the two feature classes. I have also checked with a search cursor that my feature layer contains the values I would like to transfer.

arcpy.Append_management(mgmtTractFL, db_mgmtTractFC, 'NO_TEST')

The documentation for the Append tool states this:

Map layers can be used as Input Datasets. If a layer has a selection, only the selected records (features or table rows) are used by the Append tool.

So why doesn't it work?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MKF62
by
Occasional Contributor III

So I had assumed that when you use "NO_TEST" it would default to using whatever common fields are there. Turns out you have to actually use field map objects. So this is what I ended up with:

fieldmap = arcpy.FieldMappings()
fieldmap.addTable(db_mgmtTractFC)
fieldmap.addTable(mgmtTractFL)
fieldmap.removeAll()
fm_TractID = arcpy.FieldMap()
fm_TractID.addInputField(db_mgmtTractFC, "MgmtTractID")
#I had a joined table,so I had to specify the field by putting the name of 
#the layer at the beginning of the field name
fm_TractID.addInputField(mgmtTractFL, "AL_MgmtTracts_Template.MgmtTractID")
fieldmap.addFieldMap(fm_TractID)
arcpy.Append_management(mgmtTractFL, db_mgmtTractFC, 'NO_TEST', fieldmap)

View solution in original post

0 Kudos
4 Replies
MKF62
by
Occasional Contributor III

So I had assumed that when you use "NO_TEST" it would default to using whatever common fields are there. Turns out you have to actually use field map objects. So this is what I ended up with:

fieldmap = arcpy.FieldMappings()
fieldmap.addTable(db_mgmtTractFC)
fieldmap.addTable(mgmtTractFL)
fieldmap.removeAll()
fm_TractID = arcpy.FieldMap()
fm_TractID.addInputField(db_mgmtTractFC, "MgmtTractID")
#I had a joined table,so I had to specify the field by putting the name of 
#the layer at the beginning of the field name
fm_TractID.addInputField(mgmtTractFL, "AL_MgmtTracts_Template.MgmtTractID")
fieldmap.addFieldMap(fm_TractID)
arcpy.Append_management(mgmtTractFL, db_mgmtTractFC, 'NO_TEST', fieldmap)
0 Kudos
DougBrowning
MVP Esteemed Contributor

NO_TEST always brought over the common fields just fine for me.  I use it a lot actually.  I am trying to remember if a Join messed it up.

0 Kudos
JoeBorgione
MVP Emeritus

I started to reply to your original post, but when I posted, you'd already deleted it.  So I'll try again now...  

I take it you are using a feature layer, mgmtTractFL because you've made a selection via a script, right?  And you mentioned it worked with the tool interface but not with arcpy.Append_management.  When it is successful with the tool are you using the feature layer, or the actual feature class?  It does seem odd, indeed....

I'll see if I can replicate the problem here locally with something on hand....

hahaha.  now you've beaten me to the punch again!  Good job on using field mapping.  I've never had that work for me, and I've never known anyone to get it to work!

That should just about do it....
0 Kudos
MKF62
by
Occasional Contributor III

Field mapping is super confusing in arcpy. It took me a while to figure it out switching back and forth between the FieldMappings and FieldMap help documents.

0 Kudos