Add function list relationship classes

1704
8
06-03-2021 10:25 AM
Status: Open
LukeSavage
Occasional Contributor II

Add a function called ListRelationshipClasses().  That way, we can grab these objects easier than using a bunch of desc language to get to it.

8 Comments
JoeBorgione

Awesome idea! 

JohannesLindner
  1. good, idea, upvoted.
  2. in the meantime, you can do this:

 

 

def listRelationshipClasses(database):
    """Returns a list of dictionaries describing the relationship classes inside the given database.

    Tested with SDE and FGDB

    database: str, path to the database

    """
    desc = arcpy.da.Describe(database)
    parents = [desc] + [c for c in desc["children"] if c["dataElementType"] == "DEFeatureDataset"]
    rs = []
    for p in parents:
        rs += [c for c in p["children"] if c["dataElementType"] == "DERelationshipClass"]
    return rs


listRelationshipClasses("Path\to\your\database.gdb")
listRelationshipClasses("Path\to\your\database.sde")

 

 

 

JoeBorgione

Nice @JohannesLindner !

LukeSavage

Thank you @JohannesLindner for the info.  I'm having a hard time traversing through a dataset with the script you provided as I've seen examples like that in other blogs.  I was posting this for more control over searching in a dataset, or standalone and looping through all of them or specifying specific keywords to choose.  This is key to help manage any changes to Utility Networks too as relationships are warehoused within a feature dataset.  I'm starting to switch our data over to some of the new models and this function will help manage them with the recreation of relationships when schema changes are massive.  Eventually, I'm hopeful that UN will become less strict on how its managed in schema as long as we meet certain criteria.  Also, branch versioning has very strict rules before publishing so this function helps in deleting and recreating just in case globalIDs aren't populated correctly even though ArcGIS Pro or Python says, successful.  I've had major issues with preparing branch versioning geodatabases because of the massive issues with operations successfully completing when they really haven't.  It's just a nice feel good message.  Hopefully, esri Fixes these items in future releases because when I run routines, I'd like to think the successful message is accurate.

JohannesLindner

Updated the code in my comment above: Now it also finds relationship classes in feature datasets, and it returns a list of dicts instead of the old describe objects, so it's easier to read the output.

 

Not sure if this covers some of what you're looking for:

 

import os, pprint

all_rs = listRelationshipClasses("path/to/your/database.sde")  # see above

pprint.pprint(all_rs[0])

# filter by name
rs = [r for r in all_rs if r["name"] == "FilterName"]
rs = [r for r in all_rs if "PartOfTheName" in rs["name"]]

# filter by cardinality
rs = [r for r in all_rs if r["cardinality"] == "OneToOne"]

# filter by origin or destination table
rs = [r for r in all_rs if "OriginTableName" in r["originClassNames"]]
rs = [r for r in all_rs if "DestinationTableName" in r["destinationClassNames"]]
rs = [r for r in all_rs if "OriginTableName" in r["originClassNames"] and "DestinationTableName" in r["destinationClassNames"]]

# filter by dataset
rs = [r for r in all_rs if os.path.basename(r["path"]) == "DatasetName"]

# filter by flags
rs = [r for r in all_rs if r["isComposite"]]
rs = [r for r in all_rs if not r["isComposite"]]
# other flags: isAttachmentRelationship, isAttributed, isReflexive, isVersioned, canVersion, changeTracked

 

LukeSavage

@JohannesLindner Thank you and will check it out.

HannesZiegler

Hi everyone,

Thank you for the idea and discussion around alternatives!

I wanted to highlight that this can also be done using arcpy.da.Walk:

 

import os

ws = r"C:\<my_path>"
rlclss = []
walk = arcpy.da.Walk(ws, "RelationshipClass")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        rlclss.append(os.path.join(dirpath, filename))

see arcpy.da.Walk for more info.

 

HannesZiegler
Status changed to: Open

Reset status to "Open"