Python - Relationship Classes

5766
5
06-13-2011 03:01 PM
AlvaroMoreno
New Contributor
Hi,
How do i list the Relationship Classes with python ?
For tables, datasets and featuresClass use ListTables, ListDatasets and ListFeatureClasses.
Thanks
0 Kudos
5 Replies
DavidWynne
Esri Contributor
Hi,
How do i list the Relationship Classes with python ?
For tables, datasets and featuresClass use ListTables, ListDatasets and ListFeatureClasses.
Thanks


Hi Gonzalo,
Yes, it's possible using Describe and it's children property.  children will return a list of subelements from a workspace. 

So, if you use a bit of code like below, this will return a list of relationship class names.

workspace = "c:/data/gdb.gdb"
rc_list = [c.name for c in arcpy.Describe(workspace).children if c.datatype == "RelationshipClass"]


-Dave
0 Kudos
MelanieRosenberg2
Occasional Contributor II

I have consulted the post above, but found that the solution is out of date and no longer works.

import arcpy, os workspace = r'C:/Users/OWNER/Desktop/test.gdb'# Generate the list of relationship classesrc_list = [c.name for c in arcpy.Describe(workspace).children if c.datatype == "RelationshipClass"]print rc_list

This returned "[]" which is an empty array. This means this is no longer a viable option since I have literally 150 relationship classes to rename and none were returned.

Eventually I want to rename my relationship classes as such:

import arcpy, osfrom arcpy import env # Set workspaceenv.workspace = "C:/Users/XXX/Desktop/test.gdb" #get list of relationship classesrc_list = [c.name for c in arcpy.Describe(env.workspace).children if c.datatype == "RelationshipClass"]   #remove all underscoresfor rc in rc_list:    out_data = rc.replace("_","")    arcpy.Rename_management(os.path.join(env.workspace,rc), out_data)#get the list of relationship classes againrc_list = [c.name for c in arcpy.Describe(env.workspace).children if c.datatype == "RelationshipClass"] for rc in rc_list:       #    if "reading" in rc OR "action" in rc:        #add underscores between name and ATTACHREL        if "ATTACH" in rc:            out_data = rc.replace("ATTACHREL","TblGPS__ATTACHREL")            arcpy.Rename_management(os.path.join(env.workspace,rc), out_data)            print (rc)         #add underscores between name and rel        if "rel" in rc:            out_data = rc.replace("rel","TblGPS__rel")            arcpy.Rename_management(os.path.join(env.workspace,rc), out_data)            print (rc)    else:        #add underscores between name and ATTACHREL        if "ATTACH" in rc:            out_data = rc.replace("ATTACHREL","PtGPS__ATTACHREL")            arcpy.Rename_management(os.path.join(env.workspace,rc), out_data)            print (rc)         #add underscores between name and rel        if "rel" in rc:            out_data = rc.replace("rel","PtGPS__rel")            arcpy.Rename_management(os.path.join(env.workspace,rc), out_data)            print (rc)

How can I extract the relationship classes from the geodatabase so I can rename them in bulk? I have them in a file geodatabase right now but eventually I will have them in SDE.

0 Kudos
Michael_RMiller
New Contributor II

Dwynne from Esri posted a bit of code on another post that might help. Cf.:

arcpy.ListRelationshipClasses() | GeoNet 

0 Kudos
AlvaroMoreno
New Contributor
Hi,
In my case the workspace is of type RemoteDatabase.
looking at the help but not can find that case
you can pass me the code in this case ?
thanks

gonzalo
0 Kudos
DebbieBull
New Contributor II

Gonzalo, 

I think you just need to specify your SDE Connection File rather than the .gdb path
workspace = r"DatabaseConnections\Connection to dbInstance.sde" or some such.

Debbie

0 Kudos