cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Add an FK reference using PD scripting

Former Member
0 Likes
1,774

I have the columns defined in the (parent/child) tables.  The parent table has a single column PK.  How do I use PD scripting to add a reference from the child to the parent and to identify one of the existing child table columns to be used in the reference?

Thanks in advance for the help.

View Entire Topic
former_member200945
Contributor
0 Likes

Hi Chak,

The following is code sample showing how to create reference between two tables.

Here assuming you have table Parent and Child.

As you can see, the code automatically add Parent PK column as FK in child table.

set model =ActiveModel

set parentTable = model.findChildByName("Parent", cls_Table)
set childTable = model.findChildByName("Child", cls_Table)

set references=Model.references
set ref=references.createNew()
ref.name="child To Parent"
ref.code="childToParent"
set ref.object1=Childtable
set ref.object2=Parenttable


ActiveDiagram.AttachAllObjects

If you have multiple tables to reference to one table, you can modify above code.

set model =ActiveModel

set parentTable = model.findChildByName("Parent", cls_Table)

set tables=model.Tables

for each t in tables

   if  t.name <> parentTable.name then

       set references=Model.references

       set ref=references.createNew()

       ref.name=t.name & " " & parentTable.name

       ref.code=t.code & "_" & parentTable.code

       set ref.object1=t

       set ref.object2=parentTable

   end if

next

ActiveDiagram.AttachAllObjects

You can study code example(which locates at your Powerdesigner folder) to get idea

on how to call API.

Former Member
0 Likes

Thanks Phillip.  What would the code be like if I was to use an existing column in the child table to form the reference?

I did look at some sample code.  It is likely that I have yet to figure how to use the documentation.  But the metamodel class "Model" shows the following operations:

Where would I find the method findChildByName that you used in your code above?

And then looking up Reference and ReferenceJoin showed the following operations:

From my reply to an earlier posting, you can see that I have moved forward using Excel import.  But I would like to learn to use scripting to achieve the same.  More importantly, I would like to use the documentation properly.  There must have been obvious that I am not seeing.

Thanks again for your help.