How to get the relationship between feature classes by arcpy or sql

666
7
05-19-2023 12:37 PM
spiderman90
Occasional Contributor

Hello , 

I need to get a list of relationships between all feature classes with origin name and destination name 

I am still new in arcpy ,so I need help 

i would like to have script to do it by arcpy and I am using arcmap .

and I would like to ask if there is queries that get the relationship between feature classes by sql developer ? 
thanks in advance 

0 Kudos
7 Replies
JohannesLindner
MVP Frequent Contributor

My code from this Idea:

 

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")

 

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

Have a great day!
Johannes
0 Kudos
spiderman90
Occasional Contributor

Sorry for asking 

but should I add only the path of sde and the code above and it should works but where should does save the results ? 
How will the result appear for me ? 

sorry am not familiar with working with Python in arcmap ,and I am not sure how to let this code and get the result .

0 Kudos
JohannesLindner
MVP Frequent Contributor

The function will return a list of dictionaries that describe your relationship classes. You can analyse this list in the Python Window, but if you want to save it, you'll have to write extra code. Something like this:

database = "G:/ArcGIS/projects/Test/Test.gdb"
out_csv = "G:/ArcGIS/projects/Test/Relationships.csv"

all_rs = listRelationshipClasses(database)

lines = ["Name", "Origin", "Destination", "Cardinality", "Composite"]
for rs in all_rs:
    lines.append(  [rs["name"], rs["originClassNames"][0], rs["destinationClassNames"][0], rs["cardinality"], str(rs["isComposite"])] )

with open(out_csv, "w") as f:
    for line in lines:
        f.write(",".join(line) + "\n")

Have a great day!
Johannes
0 Kudos
JohannesLindner
MVP Frequent Contributor
0 Kudos
spiderman90
Occasional Contributor

i have tried the last code at this page with sql developer but it does not work with me .

Did it work with u ? Do u have idea ,how could I write correct code in sql and let it work to find the relationships between feature classes ?

0 Kudos
JohannesLindner
MVP Frequent Contributor

I have no idea, that's just the official documentation I found for this.


Have a great day!
Johannes
0 Kudos
spiderman90
Occasional Contributor

It is not working but thanks for reply 

0 Kudos