I would like to be able to detect relationship classes in a file GDB. Because users of my script might have only an ArcView level license, they would not be able to manipulate the schema of a feature class (specifically to add a field) that is in a workspace that has relationship classes. How do I detect the presence of relationship classes, so that I can document them, avoid them programatically, and allow the script to continue?
relationshipClassNames property is supposed to do this but it does not seem to work for me (tested in a file geodatabase, created relationship class between two feature classes, checked the property, the list returned is empty for both). Maybe it will work for you.
As per @blah238 suggestion, this python code lists all relationship classes within a Geodatabase and put them in a unique list (relClasses):
inGDB = r"D:\mygeodatabase.gdb" env.workspace = inGDB #################Getting all Tables and Feature Classes########### fcs =  #root of workspace for item in arcpy.ListFeatureClasses("*"): fcs.append(item) for item in arcpy.ListTables("*"): fcs.append(item) fds = arcpy.ListDatasets("*","Feature") for fd in fds: env.workspace = inGDB +'\\'+fd for fc in arcpy.ListFeatureClasses("*"): fcs.append(fd+'/'+fc) for tb in arcpy.ListTables("*"): fcs.append(fd+'/'+tb) env.workspace = inGDB relClasses = set() for i,fc in enumerate(fcs): desc = arcpy.Describe(fc) for j,rel in enumerate(desc.relationshipClassNames): relDesc = arcpy.Describe(rel) if relDesc.isAttachmentRelationship: continue relClasses.add(rel) print relClasses
I struggled with this until realizing arcpy sees relationship classes through the tables with which they are associated. Here's a little piece of code to check for relationship class names greater than length 30:
arcpy.env.workspace = 'C:/workspace' # Local variables tables = arcpy.ListTables() # Iterate through tables in file geodatabase (workspace) for t in tables: # Get relationship class(es) associated with table desc = arcpy.Describe(t) rcs = desc.relationshipClassNames # Iterate through any relationship classes associated with current table in loop for r in rcs: if len(r) > 30: print 'Relationship class ' + r + ' has ' + str(len(r)) + ' characters.'
I am at 10.5.1 and it looks like the relationshipClassNames is giving me the list of relationship class names like it supposed to
layer = "C:\\Geodatabases\\somegeodatabase.gdb\\my_layer" desc = arcpy.Describe(layer) print desc.relationshipClassNames