Item not found in this collection error - UpdateCursor on Query Table

6520
6
01-28-2015 08:07 AM
DanEvans
Occasional Contributor II

The following code:

with arcpy.da.UpdateCursor("QueryTable", ("SHAPE", "leakage_with_pon_list_Eastings", "leakage_with_pon_list_Northings", "leakage_with_pon_list_RandomNumber")) as cursor:
  for row in cursor:
  row[1] = row[0].positionAlongLine(row[3], True).X
  row[2] = row[0].positionAlongLine(row[3], True).Y

  cursor.updateRow(row)

fails with an "item not found in this collection" error, apparently on line 02. Is it not possible to use an update cursor on a query table or something?

0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

Although I don't think this is related to the error, it looks like your indentation is off on lines 03 and 04.  Not sure if the code itself is that way or just a copy & paste problem into the text box.

Can you post the specific error output, all of it?  Knowing the line of the error is helpful, but usually additional information is output that is also helpful.

0 Kudos
DanEvans
Occasional Contributor II

The indentation is correct in the script, must just be a copy and paste problem.

The full text of the error is:

Traceback (most recent call last):

  File "D:\Design_Layer_Tools\Leakage\Code\ValidateLeakage.py", line 93, in <module>

    for row in cursor:

RuntimeError: Item not found in this collection.

(Line 93 = Line 02 of the code segment I posted).

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You mention "query table."  Are you using Make Query Table or Make Query Layer?  Make Query Table is powerful, but it is an older tool, and I am not sure if its outputs are editable.

I just did a quick test using Make Query Table where I took a single feature class and used Make Query Table to create a layer, so this is a very simple case without multiple data sets or joins.  Although I could use an update cursor on the feature class directly, I could not use it on the layer created from Make Query Table.

0 Kudos
JamesCrandall
MVP Frequent Contributor

Change this:

with arcpy.da.UpdateCursor("QueryTable", ["SHAPE", "leakage_with_pon_list_Eastings", "leakage_with_pon_list_Northings", "leakage_with_pon_list_RandomNumber"]) as cursor:  

or to this:

flds = ["SHAPE", "leakage_with_pon_list_Eastings", "leakage_with_pon_list_Northings", "leakage_with_pon_list_RandomNumber"]
with arcpy.da.UpdateCursor("QueryTable", flds) as cursor:  

(Edits applied above)

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Although using lists for field names is much more common, tuples are supported.  I am leaning more towards the query table.

JamesCrandall
MVP Frequent Contributor

I struggle with this type of stuff because python is so finicky with specifics and I never know what I can get away with

0 Kudos