Erwin API 问题向模型添加关系

问题描述 投票:0回答:1

是否有人获得使用 Erwin API 将关系添加到 Erwin 模型的示例代码。例如。添加新的 key_group。 添加实体和属性非常简单,但很难弄清楚如何添加关系。

我编写了代码来读取关键组属性并检查关系是否存在,不确定是否添加关键组和属性。

relationship erwin
1个回答
0
投票

我知道这已经晚了,但这是我在网上可以找到的使用 Erwin Data Modeller API 创建关系的唯一参考。刚刚完成并部署此内容后,我想与未来的搜索者分享。 值得注意的是,api 文档和元数据模型很有帮助,但我最终所做的是在 Erwin UI 上创建关系并将操作日志项复制到 VBA 中。该代码有点迟钝,但它应该让您了解如何解决它。 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

它保存在管理 API 连接的类中。函数 - object_exist - 仅检查当前 model_objects 中是否已存在关系。变量 - model-objects - 是根模型对象的 ModelObjects 集合(即您要添加关系的模型)

以下是用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: {" + 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

结束子

© www.soinside.com 2019 - 2024. All rights reserved.