Erwin API question adding relationships to model

Has anyone got example code to add relationships to Erwin model using Erwin API. E.g. adding a new key_group. Adding entities and attributes is pretty straightforward, struggling with figuring out how to add relationships. I have written code to read key group properties and check if relationship exists, unsure about adding key groups and properties.

28.4k 9 9 gold badges 61 61 silver badges 78 78 bronze badges asked Mar 30 at 14:23

1 Answer 1

I know this is late to the party but this was the only reference I could find online to creating relationships using the Erwin Data Modeller API. Having just finished and deployed this, I wanted to share for future searchers. To note the api docs and metadata model were helpful but what I eventually did was create a relationship on the Erwin UI and copied the action log items into VBA. The code is a bit obtuse but it should give you an idea of how to solve it. https://bookshelf.erwin.com/bookshelf/public_html/2020R2/Content/PDFs/API%20Reference.pdf https://bookshelf.erwin.com/bookshelf/9.7.00/Bookshelf_Files/PDF/erwin%20Metamodel%20Overview.pdf

The below was implemented in VBA:

Public Sub create_relationship(ByRef relationship_name As String, ByRef foreign_key_name As String, ByRef parent_table_name As String, ByRef child_table_name As String, ByRef relationship_type As String) Dim relationship_object, fk_column, fk_object, fk_group, pk_column, parent_table, child_table As SCAPI.ModelObject If object_exists(relationship_name, "Relationship") Then Exit Sub End If Debug.Print ("Creating Relationship - " + relationship_name + " - " + foreign_key_name + " - " + parent_table_name + " - " + child_table_name + " - " + relationship_type) Call Me.begin_transaction Set parent_table = model_objects.Item(parent_table_name, "Entity") Set child_table = model_objects.Item(child_table_name, "Entity") Set key_group = model_objects.Item("XPK" + parent_table_name, "Key_Group") Set relationship_object = model_objects.Add("Relationship") relationship_object.Properties("Name").Value = relationship_name relationship_object.Properties("Parent_Entity_Ref").Value = parent_table.Properties("Long_Id").Value relationship_object.Properties("Child_Entity_Ref").Value = child_table.Properties("Long_Id").Value relationship_object.Properties("Null_Option_Type").Value = "100" relationship_object.Properties("Key_Group_Ref").Value = key_group.Properties("Long_Id").Value If relationship_type = "Identifying" Then relationship_object.Properties("Type").Value = CLng(2) ElseIf relationship_type = "Non-Identifying" Then relationship_object.Properties("Type").Value = CLng(7) ElseIf relationship_type = "Many-to-Many" Then relationship_object.Properties("Type").Value = CLng(4) Else MsgBox "Error: Relationship type: is not valid - please use Identifying, Non-Identifying or Many-to-Many" End End If Set old_col = get_column_object(child_table_name, foreign_key_name) Set a = model_objects.Collect(key_group.Properties("Key_Group_Members_Order_Ref").Value) Set pk_column = model_objects.Collect(a.Root.Properties("Attribute_Ref").Value(1)).Root Set fk_group = model_objects.Collect(child_table).Add("Key_Group") fk_group.Properties("Name").Value = "XFK" + foreign_key_name + child_table_name fk_group.Properties("Key_Group_Type").Value = "IF1" fk_group.Properties("Relationship_Ref").Value = relationship_object.Properties("Long_Id").Value Set fk_column = model_objects.Collect(child_table).Add("Attribute") fk_column.Properties("Name").Value = foreign_key_name fk_column.Properties("Parent_Relationship_Ref").Value = relationship_object.Properties("Long_Id").Value fk_column.Properties("Type").Value = "100" fk_column.Properties("Parent_Attribute_Ref").Value = pk_column.Properties("Long_Id").Value Set fk_object = model_objects.Collect(fk_group).Add("Key_Group_Member") fk_object.Properties("Name").Value = foreign_key_name fk_object.Properties("Attribute_Ref").Value = fk_column.Properties("Long_Id").Value If Not relationship_type = "Many-to-Many" Then Call model_objects.Remove(old_col) End If Call Me.commit_transaction End Sub 

Notes: It is held in a class that manages the API connection.

Function - object_exist = just checks if the relationship already exists in the current model_objects.

Variable - model-objects = is the ModelObjects collection of the root model objects (i.e. the model you are adding the relationship to).

Function - get_column = Returns the requested column as a ModelObject.