<?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 Re: CreateRelationshipClass with MANY_TO_MANY - how? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666609#M74914</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3265"&gt;@TonyAlmeida&lt;/a&gt;. If I understand your code correctly, this assumes that there already is an intermediate relationship table, correct? how do you create such a table? I found&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/3.4/tool-reference/data-management/table-to-relationship-class.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/3.4/tool-reference/data-management/table-to-relationship-class.htm&lt;/A&gt;, but this gp tool too assumes there is already such an intermediate table.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Nov 2025 06:52:36 GMT</pubDate>
    <dc:creator>nadja</dc:creator>
    <dc:date>2025-11-18T06:52:36Z</dc:date>
    <item>
      <title>CreateRelationshipClass with MANY_TO_MANY - how?</title>
      <link>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666189#M74908</link>
      <description>&lt;P&gt;I'm trying to create several Relationship Classes with the cardinalities 1:1 and n:n. The below script works fine with my 1:1 relationship classes. It runs also for the n:n cardinalities, creates some relationship classes, but I can't access its values with the explore tool as with the 1:1 relationship classes. Why? What's wrong?&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for index, row in df.iterrows():
    arcpy.management.CreateRelationshipClass(origin_table = row["origin_table"],
                                            destination_table = row["destination_table"],
                                            out_relationship_class = row["out_relationship_class"],
                                            relationship_type = row["relationship_type"],
                                            forward_label = row["forward_label"],
                                            backward_label = row["backward_label"],
                                            message_direction = row["message_direction"],
                                            cardinality = row["cardinality"],
                                            attributed = row["attributed"],
                                            origin_primary_key = row["origin_primary_key"],
                                            origin_foreign_key = row["origin_foreign_key"],
                                            destination_primary_key = row["destination_primary_key"],
                                            destination_foreign_key = row["destination_foreign_key"])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with my table:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="10%" height="25px"&gt;origin_table&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;destination_table&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;out_relationship_class&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;relationship_type&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;forward_label&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;backward_label&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;message_direction&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;cardinality&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;attributed&lt;/TD&gt;&lt;TD width="5%" height="25px"&gt;origin_primary_key&lt;/TD&gt;&lt;TD width="2.5%" height="25px"&gt;origin_foreign_key&lt;/TD&gt;&lt;TD width="1.25%" height="25px"&gt;destination_primary_key&lt;/TD&gt;&lt;TD width="1.25%" height="25px"&gt;destination_foreign_key&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="10%" height="25px"&gt;mgdm_polygone_polygon&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;mgdm_polygone_regel&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;polygon_regel&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;SIMPLE&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;Regel&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;polygon&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;NONE&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;MANY_TO_MANY&lt;/TD&gt;&lt;TD width="10%" height="25px"&gt;NONE&lt;/TD&gt;&lt;TD width="5%" height="25px"&gt;Regel&lt;/TD&gt;&lt;TD width="2.5%" height="25px"&gt;Regel&lt;/TD&gt;&lt;TD width="1.25%" height="25px"&gt;RegelCode&lt;/TD&gt;&lt;TD width="1.25%" height="25px"&gt;#&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 17 Nov 2025 08:44:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666189#M74908</guid>
      <dc:creator>nadja</dc:creator>
      <dc:date>2025-11-17T08:44:58Z</dc:date>
    </item>
    <item>
      <title>Re: CreateRelationshipClass with MANY_TO_MANY - how?</title>
      <link>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666290#M74911</link>
      <description>&lt;P&gt;&lt;SPAN&gt;ArcGIS creates &lt;/SPAN&gt;intermediate relationship table&lt;SPAN&gt; that stores the pairs of related objects. Unlike 1:1 relationships, you cannot directly access related records through the Explore tool without this intermediate table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Try something like this.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy
import os

def create_relationship_with_intermediate_check(row):
    """Create relationship class and handle intermediate table for n:n"""
    
    # Create the relationship class
    arcpy.management.CreateRelationshipClass(
        origin_table=row["origin_table"],
        destination_table=row["destination_table"], 
        out_relationship_class=row["out_relationship_class"],
        relationship_type=row["relationship_type"],
        forward_label=row["forward_label"],
        backward_label=row["backward_label"],
        message_direction=row["message_direction"],
        cardinality=row["cardinality"],
        attributed=row["attributed"],
        origin_primary_key=row["origin_primary_key"],
        origin_foreign_key=row["origin_foreign_key"],
        destination_primary_key=row["destination_primary_key"],
        destination_foreign_key=row["destination_foreign_key"]
    )
    
    # Handle n:n specific setup
    if row["cardinality"].upper() == "MANY_TO_MANY":
        handle_many_to_many_relationship(row)

def handle_many_to_many_relationship(row):
    """Locate and verify the intermediate table for an n:n relationship class."""
    
    relationship_class = row["out_relationship_class"]

    print(f"n:n Relationship created: {relationship_class}")

    # Describe the relationship class
    desc = arcpy.Describe(relationship_class)

    # ArcGIS tells us exactly which table is the join table
    rel_table_path = desc.relationshipTable

    if not rel_table_path:
        print("No intermediate relationship table found (Describe returned None).")
        return

    print(f"Intermediate table located: {rel_table_path}")

    # Verify it exists
    if arcpy.Exists(rel_table_path):
        print("Intermediate relationship table verified")

        # Inspect fields
        rel_desc = arcpy.Describe(rel_table_path)
        fields = [f.name for f in rel_desc.fields]
        print(f"Relationship table fields: {fields}")
    else:
        print("Relationship table path does not exist in the workspace.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Nov 2025 15:44:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666290#M74911</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2025-11-17T15:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: CreateRelationshipClass with MANY_TO_MANY - how?</title>
      <link>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666609#M74914</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3265"&gt;@TonyAlmeida&lt;/a&gt;. If I understand your code correctly, this assumes that there already is an intermediate relationship table, correct? how do you create such a table? I found&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/3.4/tool-reference/data-management/table-to-relationship-class.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/3.4/tool-reference/data-management/table-to-relationship-class.htm&lt;/A&gt;, but this gp tool too assumes there is already such an intermediate table.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Nov 2025 06:52:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666609#M74914</guid>
      <dc:creator>nadja</dc:creator>
      <dc:date>2025-11-18T06:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: CreateRelationshipClass with MANY_TO_MANY - how?</title>
      <link>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666634#M74915</link>
      <description>&lt;P&gt;I get an error if i try to access the relationshipTable from a RelationshipClass using&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;print(rc.relationshipTable)&lt;/LI-CODE&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;AttributeError&lt;/SPAN&gt;: DescribeData: Method relationshipTable does not exist&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Nov 2025 10:34:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666634#M74915</guid>
      <dc:creator>nadja</dc:creator>
      <dc:date>2025-11-18T10:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: CreateRelationshipClass with MANY_TO_MANY - how?</title>
      <link>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666741#M74916</link>
      <description>&lt;P&gt;Yes, the code assumes the intermediate table already exists.&lt;BR /&gt;But ArcGIS automatically creates the intermediate table for you when BOTH of the following are true:&lt;/P&gt;&lt;P&gt;cardinality = "MANY_TO_MANY"&lt;/P&gt;&lt;P&gt;attributed = "NONE" (i.e., NOT attributed)&lt;/P&gt;</description>
      <pubDate>Tue, 18 Nov 2025 15:28:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666741#M74916</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2025-11-18T15:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: CreateRelationshipClass with MANY_TO_MANY - how?</title>
      <link>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666743#M74917</link>
      <description>&lt;P&gt;Try this,&lt;/P&gt;&lt;P&gt;Set the parameter values to:&lt;/P&gt;&lt;P&gt;cardinality = "MANY_TO_MANY"&lt;BR /&gt;attributed = "NONE"&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;row dictionary contains:&lt;BR /&gt;row["cardinality"] = "MANY_TO_MANY"&lt;BR /&gt;row["attributed"] = "NONE"&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy
import os

def create_relationship_with_intermediate_check(row):
    """Create relationship class and handle intermediate table for n:n"""

    # Should auto-create the join table
    if row["cardinality"].upper() == "MANY_TO_MANY":
        row["attributed"] = "NONE"  # REQUIRED for system-generated table

    # Create the relationship class
    arcpy.management.CreateRelationshipClass(
        origin_table=row["origin_table"],
        destination_table=row["destination_table"],
        out_relationship_class=row["out_relationship_class"],
        relationship_type=row["relationship_type"],
        forward_label=row["forward_label"],
        backward_label=row["backward_label"],
        message_direction=row["message_direction"],
        cardinality=row["cardinality"],
        attributed=row["attributed"],
        origin_primary_key=row["origin_primary_key"],
        origin_foreign_key=row["origin_foreign_key"],
        destination_primary_key=row["destination_primary_key"],
        destination_foreign_key=row["destination_foreign_key"]
    )

    # Handle M:N after creation
    if row["cardinality"].upper() == "MANY_TO_MANY":
        handle_many_to_many_relationship(row)

def handle_many_to_many_relationship(row):
    """Locate and verify the intermediate table for an n:n (non-attributed) relationship."""

    relationship_class = row["out_relationship_class"]
    print(f"n:n Relationship created: {relationship_class}")

    desc = arcpy.Describe(relationship_class)

    # SAFE ATTRIBUTE CHECK
    if hasattr(desc, "relationshipTable"):
        rel_table_path = desc.relationshipTable
    else:
        print("No system-generated intermediate table exists (attributed=ATTRIBUTED or invalid RC).")
        return

    if not rel_table_path:
        print("Describe returned a blank relationshipTable property.")
        return

    print(f"Intermediate table located: {rel_table_path}")

    if arcpy.Exists(rel_table_path):
        print("Intermediate table verified.")

        # List fields
        fields = [f.name for f in arcpy.ListFields(rel_table_path)]
        print(f"Relationship table fields: {fields}")
    else:
        print("Relationship table does not exist in the workspace.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Nov 2025 15:35:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/createrelationshipclass-with-many-to-many-how/m-p/1666743#M74917</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2025-11-18T15:35:19Z</dc:date>
    </item>
  </channel>
</rss>

