SearchRelatedRecords
Summary
The SearchRelatedRecords object retrieves related records based on the relationship between the origin and destination feature class or table defined in a relationship class.
The object returns an iterator of tuples. Each tuple contains two items: the first item is a row (tuple) from the origin table and the second item is a row (tuple) from the destination table.
Discussion
The SearchRelatedRecords class can be iterated using a for loop. The order that records are yielded may not match the order of the ObjectID values passed to the oids_to_search parameter. The SearchRelatedRecords object also supports with statements to aid in the removal of data locks.
The exact fields returned from each table can be controlled with the origin_fields and destination_fields parameters. Either origin_fields or destination_fields can be omitted individually, but at least one of them must be provided an argument. If one of these parameters is not provided an argument, the other must be provided an argument.
Origin and destination feature class Geometry object properties can be accessed by providing the SHAPE@ token in the respective list of fields. However, accessing full geometry with the SHAPE@ token is a computationally intensive operation. If only simple geometry information is required, such as the x, y coordinates of a point, use tokens such as SHAPE@XY, SHAPE@Z, and SHAPE@M for faster, more efficient access.
By default, the relationship is read in the forward direction. This means that the provided ObjectID values apply to the origin table and are used to find related records in the destination table. To reverse the direction, set the backward parameter to True. Now the provided ObjectID values apply to the destination table and are used to find related records in the origin table.
If no related records are found for a provided ObjectID value, no results will be yielded for that record.
Syntax
SearchRelatedRecords(relationship_class, {oids_to_search}, {origin_fields}, {destination_fields}, {backward})
| Name | Explanation | Data type |
|---|---|---|
|
relationship_class |
The path to the relationship class. |
String |
|
oids_to_search (Optional) |
A list of Use an asterisk ( The default value is "*". |
Integer |
|
origin_fields (Optional) |
A list (or tuple) of field names. For a single field, you can use a string instead of a list of strings. Use an asterisk ( Additional information can be accessed using tokens (such as If no argument is passed to
The default value is None. |
String |
|
destination_fields (Optional) |
A list (or tuple) of field names. For a single field, you can use a string instead of a list of strings. Use an asterisk ( Additional information can be accessed using tokens (such as If no argument is passed to
The default value is None. |
String |
|
backward (Optional) |
The directionality of the related record search. Set to The default value is False. |
Boolean |
Methods
reset()
Resets the iteration back to the first row.
Code sample
Use the SearchRelatedRecords class to find all the Buildings related to a set of Parcels.
import arcpy
relc = os.path.join(arcpy.env.workspace, "Parcel_Building_Rel")
oids = [2710, 1010]
with arcpy.da.SearchRelatedRecords(relc, oids, origin_fields="APN", destination_fields="BUILDING_NUMBER") as related_records:
for record in related_records:
print(record)
Use the SearchRelatedRecords class in the backward direction to find Parcels related to a set of Buildings
import arcpy
relc = os.path.join(arcpy.env.workspace, "Parcel_Building_Rel")
oids = [850, 921, 1018, 2301]
with arcpy.da.SearchRelatedRecords(relc, oids, origin_fields="APN", destination_fields="BUILDING_NUMBER", backward=True) as related_records:
for record in related_records:
print(record)
Use the SearchRelatedRecords class to retrieve attributes using tokens. This example also demonstrates omitting one of the field parameters, which can be useful when you're only interested in the related records.
import arcpy
relc = os.path.join(arcpy.env.workspace, "Parcel_Building_Rel")
oids = [2710]
with arcpy.da.SearchRelatedRecords(relc, oids, destination_fields=["OID@", "SHAPE@AREA"]) as related_records:
for record in related_records:
print(record)
Use the Python sorted function to sort records returned by SearchRelatedRecords. This example also demonstrates omitting the oids_to_search parameter, which will cause the SearchRelatedRecords class to return all related records.
import arcpy
relc = os.path.join(arcpy.env.workspace, "Parcel_Building_Rel")
for record in sorted(arcpy.da.SearchRelatedRecords(relc, origin_fields="APN", destination_fields=["BUILDING_NUMBER", "POPULATION"])):
print(record)