HOW TO

Determine if a geodatabase contains a relationship class using ArcPy

Last Published: November 26, 2020

Summary

Relationship classes can be created on a geodatabase with the Standard or Advanced ArcGIS Desktop license. Relationship classes manage the associations between objects in a feature class and a table. As there can be multiple geodatabases within a workspace, manually checking if a relationship class exists in a geodatabase can be tedious. This article provides an alternative method to determine if there is a relationship class in all the geodatabases within a workspace using ArcPy.

Procedure

  1. Import the necessary modules.
import arcpy 
from arcpy import env
  1. Specify the desired workspace.
workspace = env.workspace = r"<workspace_location>"
  1. Create a function to find the relationship classes in the specified workspace.
def detectRelationship(): 
     rc_list = [c.name for c in arcpy.Describe(workspace).children if c.datatype == "RelationshipClass"]
     rc_list 
	
     # List the names, origin, and destination of the relationship class
     for rc in rc_list: 
         rc_path = workspace + "\\" + rc 
         des_rc = arcpy.Describe(rc_path) 
         origin = des_rc.originClassNames 
         destination = des_rc.destinationClassNames 
         print ("Relationship Class: %s \n Origin: %s \n Desintation: %s" %(rc, origin, destination))
  1. Call the function, and return the results if a relationship class exists.
detectRelationship()

The following code block and image shows shows the full script and the result:

import arcpy 
from arcpy import env 

workspace = env.workspace = r"C:\Users\USER\Desktop\Test_case"

def detectRelationship(): 
     rc_list = [c.name for c in arcpy.Describe(workspace).children if c.datatype == "RelationshipClass"]
     rc_list 
     for rc in rc_list: 
         rc_path = workspace + "\\" + rc 
         des_rc = arcpy.Describe(rc_path) 
         origin = des_rc.originClassNames 
         destination = des_rc.destinationClassNames 
         print ("Relationship Class: %s \n Origin: %s \n Desintation: %s" %(rc, origin, destination))          

detectRelationship()
Image of a sample result

Article ID: 000023985

Software:
  • ArcMap
  • ArcGIS Pro 2 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options