I have a script that creates a new field for the feature class, then creates a feature layer out of that FC, does a join, calculates stuff, does selections, etc. Then I try to run a search cursor on that feature layer and it says that it cannot find the field I added. If I change the input layer from the feature layer to the feature class instead, it can find the field, so we know that field does indeed exist. I would just use the feature class but then the cursor doesn't honor my selections. I don't get why it can't find the field.
#Add MgmtTractID field
arcpy.AddField_management(mgmtTractFC, 'MgmtTractID', 'Text', '#', '#', 50)
#Create feature layer
mgmtTractFL = arcpy.MakeFeatureLayer_management(mgmtTractFC, 'mgmtTractFL')
#Do a bunch of stuff...
expression = createFIDQuery(FID_SubMaster)
arcpy.SelectLayerByAttribute_management(mgmtTractFL, 'REMOVE_FROM_SELECTION', expression)
with arcpy.da.SearchCursor(mgmtTractFL, 'MgmtTractID') as sCursor:
for row in sCursor: ####### Error here: Cannot find field MgmtTractID ########
If I change the search cursor line to search the actual feature class the feature layer is based on
with arcpy.da.SearchCursor(mgmtFC, 'MgmtTractID') as sCursor:
it works just fine and can find the field.
I wish I could list fields of a feature layer but I can't so I'm stuck on what to do and how to debug this thing.
UPDATE: I just tested this with other fields in my feature class and it can't find any of those either. I know you can use search cursors on feature layers so...