Select to view content in your preferred language

Automating "many to many" relationships? A scriptable way to do the same as "add selected to relationship" ?

870
5
04-25-2022 01:30 PM
Labels (2)
Glasnoct
Regular Contributor

I've had a lot of success forming the relationships I need between 1:1 and 1:M relationships between all my features and I have one instance left that I are stuck on. It seems that the only way I can create a link with a M:M relationship is manually via the "add selected to relationship" context option. I came across this page today that sounds like I might be able to accomplish what I want: - IRelationshipClass.CreateRelationship Method (ArcObjects .NET 10.8 SDK) (arcgis.com)

However, I do not know the first thing about how I might implement it since I've only worked with ArcPy so far. Could someone explain the simplest way to get that function working? I can generate a list of the required features that need to be related with arcpy but passing it into the linked function is beyond me.

Tags (2)
0 Kudos
5 Replies
HannesZiegler
Esri Contributor

Hi @Glasnoct, you won't be able to use the linked CreateRelationship method in Python, The SDK is for C#. 

Is this what you are looking for?

https://pro.arcgis.com/en/pro-app/2.8/tool-reference/data-management/create-relationship-class.htm

0 Kudos
Glasnoct
Regular Contributor

I know I won't be able to use it in python. I was hoping for a minimally viable bit of code that I can run independently that will do the relationship for me if I can find a way to give it a list of the objects that need relating.

The method you linked looks like it's only for creating relationship classes? These are existing classes that I need to add feature relationships to; can I do that?

0 Kudos
HannesZiegler
Esri Contributor

After looking into your question a bit further, unfortunately there is no direct support for add features to relationship.

However, there is a possible workaround using the Table To Relationship Class tool to add many-to-many relationships to a relationship class. You would need to generate the input table that stores the info about the relationships first. You should be able to generate the table with arcpy by using the selection tools and a cursor.

Hope that helps!

0 Kudos
Glasnoct
Regular Contributor

This too looks like it results in a brand new relationship class?

I'm guessing there's no way to, say, join the existing class and the new class if I manage to line up the attributes exactly the same way?

0 Kudos
HannesZiegler
Esri Contributor

Yea it seems it would not be an in-place update of an existing relationship class , but rather a replacement of the existing relationship class with the updated one. It's a bit hacky unfortunately.

select features >> add features to existing table using cursor (or create new if needed) >> create from table

0 Kudos