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?
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"])
with my table:
| origin_table | destination_table | out_relationship_class | relationship_type | forward_label | backward_label | message_direction | cardinality | attributed | origin_primary_key | origin_foreign_key | destination_primary_key | destination_foreign_key |
| mgdm_polygone_polygon | mgdm_polygone_regel | polygon_regel | SIMPLE | Regel | polygon | NONE | MANY_TO_MANY | NONE | Regel | Regel | RegelCode | # |
ArcGIS creates intermediate relationship table 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.
Try something like this.
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.")