Description

Creates a relationship. Valid relationship types: 0 = Identifying, 1 = NonIdentifying, 2 = NonSpecific, 3 = NonIdentifying Optional, 8 = Containment (MongoDB only). PARAMETERS: ParentEntity (String or Integer), ChildEntity (String or Integer), RelationshipType (Integer), PKPairList (String).

Syntax

Public Function AddWithRoleName( _
   ByVal ParentEntity As Variant, _
   ByVal ChildEntity As Variant, _
   ByVal RelationshipType As Variant, _
   ByVal PKPairList As Variant _
) As Relationship

Parameters

  • ParentEntity
  • ChildEntity
  • RelationshipType
  • PKPairList

Remarks

PKPairList syntax/rules:

  1. Each PKPairList string should be in this format "PKname1,RoleName1;PKName2,RoleName2" . PKName is the Primary Key Attribute/Column name of the Parent Entity, and the RoleName is the RoleName to use in the propagated ForeignKey. PKName and RoleName are comma separated and each pair is separated by a semi-colon.
  2. There is an escape character "\" so that you can use the separator characters inside the names. All other letters are alright to use as long as SaxBasic allows you to enter it into the parameter string. Example: Column,Role;Name has to be input as Column\,Role\;Name
  3. PK names are strictly enforced. It is case sensitive. The name has to be entered the way it is used in the model.
  4. Any PK Column Names that donʼt match to any of the Primary Keys of the ParentEntity will be automatically disregarded.

The PKPairList must follow these rules strictly. Any deviation from it will make the Relationship creation fail. Check the returned Relationship for a valid ID. Zero indicates that creation failed. In Automation, this function works well for Recursive Relationship creation because it can automatically RoleName the Foreign Keys.

Example

Sub Main

Dim diag As Diagram
Dim mod As Model
Dim rels As Relationships
Dim newrel As Relationship

Set diag = DiagramManager.ActiveDiagram
Set mod = diag.ActiveModel
Set rels = mod.Relationships

'Recursive relationship
'Entity1 or Entity2 can be specified as tablename, owner.tablename, or ID. 

Set newrel = rels.AddWithRoleName("Entity1", "Entity1", 1," attr1,attr1_role; col1, col1_role")

If newrel.ID <> 0 Then
MsgBox("Succesfully added new Relationship")
End If

End Sub