<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Inserting record in M:N relationship table &amp;quot;Cannot reset foreign key values for an existing relationship row&amp;quot; in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1110631#M62776</link>
    <description>&lt;P&gt;For the first time I'm trying to set up and use a many to many relationship. The setup went fine, as expected an "intermediate table" is create the contains two GUID fields. My problem is when I try to use ArcPY to populate that table. It seems that I have to create a new InsertCursor for every record that I want to insert. Does that make sense? This code snippet fails on the second insertRow with&amp;nbsp;"Cannot reset foreign key values for an existing relationship row". However when I uncomment out line 12, it works.&amp;nbsp; I guess I can create the insert cursor every time but I expect the table to get very very large and I seen slow cursor creation on large tables, so it may not be practical.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os, arcpy

DB_BASE_PATH = r'C:\Users\dmorrison\Documents\ArcGIS\Projects\CW Development\CW HUB (SDE).sde'
REL_TABLE =  os.path.join(DB_BASE_PATH, 'CW_Hub.SDE.TEST_PARCELS_TEST_PARCEL_TO_PARCEL_EVENTS')

P_GUID1 = "31D01E3D-F70B-4382-B841-F9DD552C1859" 
P_GUID2 = "AC8D91AC-59FD-4E90-9451-275EEF6A99E9"
E_GUID = "944E481E-4F91-44B3-A640-66383031F8D9"

with arcpy.da.InsertCursor(REL_TABLE, ['parcel_guid', 'event_guid']) as cursor:
    cursor.insertRow([P_GUID1, E_GUID])
#with arcpy.da.InsertCursor(REL_TABLE, ['parcel_guid', 'event_guid']) as cursor:
    cursor.insertRow([P_GUID2, E_GUID])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ArcGIS Pro 2.8.3&lt;/P&gt;&lt;P&gt;SQL Server&amp;nbsp;13.0.5888.11 (Windows)&lt;/P&gt;</description>
    <pubDate>Mon, 25 Oct 2021 00:55:17 GMT</pubDate>
    <dc:creator>DonMorrison1</dc:creator>
    <dc:date>2021-10-25T00:55:17Z</dc:date>
    <item>
      <title>Inserting record in M:N relationship table "Cannot reset foreign key values for an existing relationship row"</title>
      <link>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1110631#M62776</link>
      <description>&lt;P&gt;For the first time I'm trying to set up and use a many to many relationship. The setup went fine, as expected an "intermediate table" is create the contains two GUID fields. My problem is when I try to use ArcPY to populate that table. It seems that I have to create a new InsertCursor for every record that I want to insert. Does that make sense? This code snippet fails on the second insertRow with&amp;nbsp;"Cannot reset foreign key values for an existing relationship row". However when I uncomment out line 12, it works.&amp;nbsp; I guess I can create the insert cursor every time but I expect the table to get very very large and I seen slow cursor creation on large tables, so it may not be practical.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os, arcpy

DB_BASE_PATH = r'C:\Users\dmorrison\Documents\ArcGIS\Projects\CW Development\CW HUB (SDE).sde'
REL_TABLE =  os.path.join(DB_BASE_PATH, 'CW_Hub.SDE.TEST_PARCELS_TEST_PARCEL_TO_PARCEL_EVENTS')

P_GUID1 = "31D01E3D-F70B-4382-B841-F9DD552C1859" 
P_GUID2 = "AC8D91AC-59FD-4E90-9451-275EEF6A99E9"
E_GUID = "944E481E-4F91-44B3-A640-66383031F8D9"

with arcpy.da.InsertCursor(REL_TABLE, ['parcel_guid', 'event_guid']) as cursor:
    cursor.insertRow([P_GUID1, E_GUID])
#with arcpy.da.InsertCursor(REL_TABLE, ['parcel_guid', 'event_guid']) as cursor:
    cursor.insertRow([P_GUID2, E_GUID])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ArcGIS Pro 2.8.3&lt;/P&gt;&lt;P&gt;SQL Server&amp;nbsp;13.0.5888.11 (Windows)&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 00:55:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1110631#M62776</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2021-10-25T00:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting record in M:N relationship table "Cannot reset foreign key values for an existing relationship row"</title>
      <link>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1110903#M62779</link>
      <description>&lt;P&gt;Edit:&amp;nbsp; fixed the code to create cursor in the loop.&amp;nbsp; Probably is still expensive on large tables but it is less code.&lt;BR /&gt;&lt;BR /&gt;Could you loop over a list of the P_GUID's ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;guidList = ["31D01E3D-F70B-4382-B841-F9DD552C1859", "AC8D91AC-59FD-4E90-9451-275EEF6A99E9", p3, ...]
E_GUID = "944E481E-4F91-44B3-A640-66383031F8D9"

for pguid in guidlist:
    with arcpy.da.InsertCursor(...) as cursor:
        cursor.insertRow([pguid, E_GUID])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 16:35:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1110903#M62779</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-25T16:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting record in M:N relationship table "Cannot reset foreign key values for an existing relationship row"</title>
      <link>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1110958#M62780</link>
      <description>&lt;P&gt;Right, I took it out of the loop simply to demonstrate the problem that I have to create a new InsertCursor for every record that I want to insert.&amp;nbsp; I've used InsertCursors before and never had to do that.&amp;nbsp; There seems to be something different about the many to many intermediate table that causes this.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 18:17:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1110958#M62780</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2021-10-25T18:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting record in M:N relationship table "Cannot reset foreign key values for an existing relationship row"</title>
      <link>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1116714#M62980</link>
      <description>&lt;P&gt;I found an acceptable workaround for this by writing out the rows to insert into a temporary in-memory table then appending the in-memory table to the "intermediate table". At least that way I don't have to create an InsertCursor for every record that I want to add.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Nov 2021 01:07:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1116714#M62980</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2021-11-15T01:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting record in M:N relationship table "Cannot reset foreign key values for an existing relationship row"</title>
      <link>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1606287#M74042</link>
      <description>&lt;P&gt;I can confirm this problem still exists in ArcGIS Pro 3.4.0, and the workaround you guys came up with of opening a new cursor for each row you want to insert still works.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Apr 2025 22:44:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1606287#M74042</guid>
      <dc:creator>JasonRoberts</dc:creator>
      <dc:date>2025-04-15T22:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting record in M:N relationship table "Cannot reset foreign key values for an existing relationship row"</title>
      <link>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1671510#M74972</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I can confirm this problem still exists in ArcGIS Pro 3.6.0 and workaround works&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Dec 2025 11:51:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/inserting-record-in-m-n-relationship-table-quot/m-p/1671510#M74972</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2025-12-09T11:51:27Z</dc:date>
    </item>
  </channel>
</rss>

