2

Batch Renaming Relationship Classes

I have consulted the post above, but found that the solution is out of date and no longer works. It suggests two options to initially get the list of relationship classes:

Option 1

import arcpy
import os

desc = arcpy.Describe(r'C:\Users\<username>\Documents\ArcGIS\Default.gdb')
print desc
for child in desc.children:
    if child.datatype == "RelationshipClass":
        print child

Option 2

import arcpy, os
workspace = r'C:\Users\OWNER\Documents\ArcGIS\Default.gdb'

# Generate the list of relationship classes
rc_list = [c.name for c in arcpy.Describe(workspace).children if c.datatype == "RelationshipClass"]
print rc_list

Both options returned "[]" which is an empty array. This means these are no longer viable options 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, os
from arcpy import env

# Set workspace
env.workspace = "C:/Users/XXX/Desktop/test.gdb"


#get list of relationship classes
rc_list = [c.name for c in arcpy.Describe(env.workspace).children if c.datatype == "RelationshipClass"]  

#remove all underscores
for 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 again
rc_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

describe (path).dataElementType returns DERelationshipClass for my relationship classes.

Try this instead to be robust:

import arcpy
import os

desc = arcpy.Describe(r'C:\Users\<username>\Documents\ArcGIS\Default.gdb')
print desc
for child in desc.children:
    if "RELATIONSHIPCLASS" in child.datatype.upper ():
        print child.catalogPath
  • The result: "<geoprocessing describe data object object at 0x0D04EC68>" – Stella Dec 21 '17 at 22:35
  • That’s the describe object. Do you have print child instead of print child.catalogPath? – Emil Brundage Dec 21 '17 at 22:39
  • No, I copied and pasted exactly what you wrote but changed the workspace – Stella Dec 21 '17 at 22:46
  • What exactly do you mean by your result? In my code the variable child will be a describe object. That is, if you print it, it will look like <geoprocessing describe data object object at 0x0D04EC68>. – Emil Brundage Dec 26 '17 at 21:12
  • Turns out my database was some how corrupted and thus, the function was not giving me the relationship classes. – Stella Jan 3 '18 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.