Feature Layer saying field does not exist when it does

1667
1
Jump to solution
02-07-2019 11:47 AM
MKF62
by
Occasional Contributor III

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...
#...
#...

#Make selections
arcpy.SelectLayerByAttribute_management(mgmtTractFL, 'NEW_SELECTION')
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 ########
      print row[0]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

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... 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MKF62
by
Occasional Contributor III

Alright, I figured this out. It had to do with the fact that I joined the layer prior to trying to use a search cursor on it. When you join tables, the name of the feature class get appended to the beginning of the field name so you can tell which field goes to which table (ex: MgmtTracts.MgmtTractID). When I change my search cursor line to specify it with the feature class name

with arcpy.da.searchCursor(mgmtTractFL, 'MgmtTracts.MgmtTractID') as sCursor:

it worked.

View solution in original post

0 Kudos
1 Reply
MKF62
by
Occasional Contributor III

Alright, I figured this out. It had to do with the fact that I joined the layer prior to trying to use a search cursor on it. When you join tables, the name of the feature class get appended to the beginning of the field name so you can tell which field goes to which table (ex: MgmtTracts.MgmtTractID). When I change my search cursor line to specify it with the feature class name

with arcpy.da.searchCursor(mgmtTractFL, 'MgmtTracts.MgmtTractID') as sCursor:

it worked.

0 Kudos