#Name: Multiple Field Key to Single Field Key for Relates using Python and ArcPy
#Author: Bryan McIntosh
#Description: For updating a single LINK_ID field in both the parent and child table
# based on a natural 4 column composite index relationship.
import arcpy
## VARIABLES ##
edit_ws = r'C:\SpatialTimes.gdb' #Edit workspace
xs_fc = r'C:\SpatialTimes.gdb\CrossSection' #parent FC
xs_tbl = r'C:\SpatialTimes.gdb\CrossSection_RatingCurves' #related table
xs_att = ['Link_ID','F1','F2','F3','F4','OID@']
## FUNCTIONS ##
def fnUpdateXSTBL(qry, linkID):
with arcpy.da.UpdateCursor(xs_tbl,xs_att, qry) as cursor2:
for row in cursor2:
row[0] = linkID
cursor2.updateRow(row)
## MAIN CODE ##
edit = arcpy.da.Editor(edit_ws) #Set the edit workspace
edit.startEditing(False, False) #Note: change second param to true if using SDE WS
with arcpy.da.UpdateCursor(xs_fc, xs_att) as cursor:
for row in cursor:
row[0] = row[5] #Makes the LinkID equal to the current ObjectID of parent
cursor.updateRow(row) #Update the parent row
#Set the query for the multi-field natural key (fields are F1, F2, F3, F4)
xs_tbl_qry = xs_att[1] + " = '{0}' AND ".format(row[1]) + xs_att[2] + " = '{0}' AND ".format(row[2]) + xs_att[3] + " = '{0}' AND ".format(row[3]) + xs_att[4] + " = {0}".format(row[4])
fnUpdateXSTBL(xs_tbl_qry, row[5]) # Send the query and parent ObjectID to update the related table
edit.stopEditing(True)