Select to view content in your preferred language

Arcpy error: updating attribute values in relationship class table

1097
2
06-21-2018 09:20 AM
EricWerfel
Emerging Contributor

I'm attempting to update the attribute values in a relationship class table using python. basically, i'm looking to update two count fields to see how many times each primary key occurs in the table, and then set a 'matchType', ie is it '1 to 1', '1 to many', etc...  here is my code: 

import arcpy

arcpy.env.workspace = 'C:\Users\Werfel\Desktop\Philly PIN\matches.gdb'

dor = {}
pwd = {}

with arcpy.da.SearchCursor('dor_pwd',['DOR_OBJECTID','PARCELID']) as cursor:
    for row in cursor:
        if row[0] not in dor.keys():
            dor[row[0]] = 1
        else:
            dor[row[0]] += 1
        
        if row[1] not in pwd.keys():
            pwd[row[1]] = 1
        else:
            pwd[row[1]] += 1

#edit = arcpy.da.Editor(arcpy.env.workspace)
#edit.startEditing(False, False)
#edit.startOperation()
            
with arcpy.da.UpdateCursor('dor_pwd',['DOR_OBJECTID','PARCELID','dorCount','pwdCount','matchType']) as cursor:
    for row in cursor:
        row[2] = dor[row[0]]
        row[3] = pwd[row[1]]
        if dor[row[0]]==1 and pwd[row[1]]==1: row[4]='1-1'
        if dor[row[0]]==1 and pwd[row[1]]>1: row[4]='M-1'
        if dor[row[0]]>1 and pwd[row[1]]==1: row[4]='1-M'
        if dor[row[0]]>1 and pwd[row[1]]>1: row[4]='M-M'
        cursor.updateRow(row)

#edit.stopOperation()
#edit.stopEditing(True)

it crashes on line 32, with the message:

RuntimeError: Cannot reset foreign key values for an existing relationship row

thing is, none of the rows i'm updating are foreign keys. row[0] and row[1] are, but i'm only reading them.  is it bc the UpdateCursor retrieves these fields? 

i can think of other ways to do this (summary table, for example), but this is by far the fastest that i came up with, and i would like to run this every time i create a relationship, of which there will be thousands

fyi, the commented lines are to indicate that i have i tried with and without an edit session

any ideas?? thanks in advance,

0 Kudos
2 Replies
ValCannon2
Occasional Contributor

I'm getting the same error in ArcGIS Pro but if I close the application and run the script in PyCharm (or IDLE, I assume) it runs great. This looks like a bug with a workaround for me.

0 Kudos
DougBrowning
MVP Esteemed Contributor

You could not use a Update Cursor.

What I like to do is

Copy to in_memory
           arcpy.TableToTable_conversion(inDir + "\\" + inDB + "\\" + table, "in_memory", table)

Add a field

Calc the new field

Join back to original FC or table

Calc over the value

Now if it is a web map then you could just use Arcade and it would be love time dynamic.

Hope that helps.

0 Kudos