Select to view content in your preferred language

Unable to insert rows into a M-N relationship table via arcpy script

292
1
Jump to solution
2 weeks ago
ArshiyaNisa
Occasional Contributor

We are trying to insert rows into a many to many relationship table via arcpy script. Attempting to write new rows with insertcursor causes an error. Manually adding data to relationship table works fine.

RuntimeError: Message: Unable to complete operation.
Details: Internal error during object insert. Invalid foreign key value

We have checked the following things-

  1. GlobalIDs existing in both the parent tables.
  2. GlobalIDs are valid.

Below is the sample snippet of code-

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.activeMap

mc_guid = '{0196282F-17E7-4A29-A2E4-0B381C407A30}'
ls_guid = '{5FB63569-5792-42H7-AAA9-3E7A912F3226}'
lsmc_rel = m.listTables("R_Table1_Table2)[0]

with arcpy.da.InsertCursor(lsmc_rel, ['T1_GlobalID', 'T2_GlobalID']) as cursor:
cursor.insertRow((mc_guid, ls_guid))

Version details- 

Enterprise Version- 11.5

ArcGIS Pro- 3.5

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
ArshiyaNisa
Occasional Contributor

This error was found and there's no proper solution for it yet.

160271: Invalid foreign key value | ArcGIS Pro documentation

We've tried ArcGIS API for Python instead of Arcpy for editing features and it worked-

import arcpy
from arcgis.gis import GIS
import arcgis
from arcgis.features import FeatureLayer
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.activeMap
gis = GIS("home")
 
# Relationship table from the map
rel_tbl = m.listTables("R_Table1_Table2")[0]
cp = rel_tbl.connectionProperties
service_url = arcpy.Describe(rel_tbl).catalogPath
version_name = "Username.test_Version"  #Change to your version name
 
fl = FeatureLayer(service_url, gis)
 
result = fl.edit_features(
    adds=[
        {
            "attributes": {
                "T1_GlobalID": "{0196282F-17E7-4A29-A2E4-0B381C407A30}",
                "T2_GlobalID": "{5FB63629-5792-42A5-AAA9-3E7A912F3226}"
            }
        }
    ],
    gdb_version= version_name
)

print(result)

View solution in original post

0 Kudos
1 Reply
ArshiyaNisa
Occasional Contributor

This error was found and there's no proper solution for it yet.

160271: Invalid foreign key value | ArcGIS Pro documentation

We've tried ArcGIS API for Python instead of Arcpy for editing features and it worked-

import arcpy
from arcgis.gis import GIS
import arcgis
from arcgis.features import FeatureLayer
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.activeMap
gis = GIS("home")
 
# Relationship table from the map
rel_tbl = m.listTables("R_Table1_Table2")[0]
cp = rel_tbl.connectionProperties
service_url = arcpy.Describe(rel_tbl).catalogPath
version_name = "Username.test_Version"  #Change to your version name
 
fl = FeatureLayer(service_url, gis)
 
result = fl.edit_features(
    adds=[
        {
            "attributes": {
                "T1_GlobalID": "{0196282F-17E7-4A29-A2E4-0B381C407A30}",
                "T2_GlobalID": "{5FB63629-5792-42A5-AAA9-3E7A912F3226}"
            }
        }
    ],
    gdb_version= version_name
)

print(result)

0 Kudos