Create Relationship with Script (PERRelation.ChangeKey)?

I'm trying to loop through all entities in a model, adding a new attribute, which is a FK to an existing table. First I add the attribute, then I create a relation, but the child key on the relation defaults to adding a new field called ID. If I try to change the child key on the relation using the ChangeKey function (added in 7.2), I get an error that the "interface is not supported". I assume this means I'm passing a parameter of the wrong type, but I have on clue what it wants.

Can someone please explain how to use this ChangeKey procedure, or if there is a better way to achieve what I'm trying to do?


This previous forum post was the catalyst for PERRelation.ChangeKey being added per task TMB-2519 in version 7.2.

Class: PERRelationAN17
Procedure: ChangeKey
Parameters: ANewKey: Dispatch


function main()
  var i, Entity, Attribute, Relationship, EntityUsers;

  // loop through all entities on the model
  for (i = 0; i < Model.Entities.Count; i++)
    Entity = Model.Entities.GetObject(i);   
    if (Entity.Name != 'EntityUsers')
      // Add the attribute that will be the fk to the existing EntityUsers table
      Attribute = Entity.CreateNewObject( 2003 );   
      Attribute.Name = 'InsertedByEntityUserID';
      Attribute.Caption = Attribute.Name;
      // Get the EntityUsers table that already exists on the model                
      EntityUsers = Model.Entities.GetObjectByName('EntityUsers');

      // Create a relationship between the EntityUsers table and the current entity
      Relationship = Model.AddLinkObject(2004, EntityUsers, Entity); // Relation     
      Relationship.Identifying = false;
      Relationship.Name = Entity.Name + Attribute.Name;   
      Relationship.Caption = Relationship.Name;      

      // Change the child key to the InsertedByEntityUserID attribute?
      // Internal error: Interface not supported




changeKey method accept key of parent entity. It changes key wchich is propagated by relationship.
You need use method ChangeChildAttribute of ForeignKey.

         var fk = Relationship.ForeignKeys.GetObject(0);  


1 Like

Thanks Petr, that worked perfectly.