arcpy.da cursors and layer definition query

04-08-2013 04:04 AM
Occasional Contributor
In my mxd I found that if I ran an arcpy.da search cursor with a where clause on a layer that had a definition query applied to it, that the where clause would not be honored. Anybody else have this problem?

Tags (2)
0 Kudos
3 Replies
Regular Contributor II
How do you know that you referenced the layer and not the underlying featureclass? If they have the same name (they do by default) then it might not be clear.
0 Kudos
Occasional Contributor
I'm having the same problem.  Someone reported the bug on May 8 (NIM-091487), but there is no information regarding when/if it will be fixed.

Simple workaround:
Include the layer's definition query in the cursor's "where clause" and refer to the layer.dataSource instead of the layer itself when creating the cursor.  For example:

This doesn't work:
MRDroutelogEvents.definitionQuery = "\"TWN_TID\" = '%s'" %twnlr
ghost_query = "\"SR_CATEG\" = 'GHOST'" %twnlr
ghost_points = sorted([item[0] for item in (arcpy.da.SearchCursor(MRDroutelogEvents, ["SR_CATEG"], ghost_query))])

This does work:
ghost_query = "\"TWN_TID\" = '%s' AND \"SR_CATEG\" = 'GHOST'" %twnlr
ghost_points = sorted([item[0] for item in (arcpy.da.SearchCursor(MRDroutelogEvents.dataSource, ["SR_CATEG"], ghost_query))]) #(doesn't matter if there is a definition query on MRDroutelogEvents or not)
Esri Frequent Contributor
Yes, I logged the following bug:

NIM091487: The "where clause" in data access cursors is not respected if there is a definition query with more than one argument (complex expression).

There is unfortunately no indication on when this issue will be addressed. If you log a support ticket and create an incident, we can get you attached to the bug and you can track it on your customer care portal.
0 Kudos