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.
<SPAN class="comment token">#Add MgmtTractID field</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>mgmtTractFC<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MgmtTractID'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Text'</SPAN><SPAN class="punctuation token">,</SPAN> '<SPAN class="comment token">#', '#', 50)</SPAN>
<SPAN class="comment token">#Create feature layer</SPAN>
mgmtTractFL <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>mgmtTractFC<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'mgmtTractFL'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Do a bunch of stuff...</SPAN>
<SPAN class="comment token">#...</SPAN>
<SPAN class="comment token">#...</SPAN>
<SPAN class="comment token">#Make selections</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'NEW_SELECTION'</SPAN><SPAN class="punctuation token">)</SPAN>
expression <SPAN class="operator token">=</SPAN> createFIDQuery<SPAN class="punctuation token">(</SPAN>FID_SubMaster<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'REMOVE_FROM_SELECTION'</SPAN><SPAN class="punctuation token">,</SPAN> expression<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MgmtTractID'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> sCursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> sCursor<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">####### Error here: Cannot find field MgmtTractID ########</SPAN>
<SPAN class="keyword token">print</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>If I change the search cursor line to search the actual feature class the feature layer is based on
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>mgmtFC<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MgmtTractID'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> sCursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
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...