# Create Relationship with Script (PERRelation.ChangeKey)?

**URL:** <https://forums.toadworld.com/t/create-relationship-with-script-perrelation-changekey/55977>\
**Category:** Toad Data Modeler\
**Created:** [September 1, 2022, 7:08pm UTC](https://forums.toadworld.com/t/create-relationship-with-script-perrelation-changekey/55977 "2022-09-01T19:08:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dquimby](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dquimby](https://forums.toadworld.com/u/dquimby)\
**Post date:** [September 1, 2022, 7:08pm UTC](https://forums.toadworld.com/t/create-relationship-with-script-perrelation-changekey/55977/1 "2022-09-01T19:08:27Z")

</div>

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?

Thanks.

[This previous forum](https://forums.toadworld.com/t/how-to-create-relationship-with-script/49073) post was the catalyst for PERRelation.ChangeKey being added per task TMB-2519 in version 7.2.

Class: PERRelationAN17  
Procedure: ChangeKey  
Parameters: ANewKey: Dispatch

Code:

```auto
function main()
{
  var i, Entity, Attribute, Relationship, EntityUsers;
  
  Model.Lock();   

  // loop through all entities on the model
  for (i = 0; i < Model.Entities.Count; i++)
  {
    Entity = Model.Entities.GetObject(i);   
    
    if (Entity.Name != 'EntityUsers')
    {
      Entity.Lock();               
            
      // Add the attribute that will be the fk to the existing EntityUsers table
      Attribute = Entity.CreateNewObject( 2003 );   
      Attribute.Name = 'InsertedByEntityUserID';
      Attribute.Caption = Attribute.Name;
      Attribute.ChangeDataType('uniqueidentifier');   
                
      // 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
      Relationship.ChangeKey(Attribute);

      Entity.UnLock();
    }
  }

  Model.UnLock();
}

  

```

---

<div class="post-metadata">

**Author:** ![petr.daricek](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@petr.daricek](https://forums.toadworld.com/u/petr.daricek)\
**Post date:** [September 5, 2022, 10:42am UTC](https://forums.toadworld.com/t/create-relationship-with-script-perrelation-changekey/55977/2 "2022-09-05T10:42:48Z")

</div>

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

```auto
         var fk = Relationship.ForeignKeys.GetObject(0);  
         fk.ChangeChildAttribute(Attribute);

```

Daril

---

<div class="post-metadata">

**Author:** ![dquimby](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dquimby](https://forums.toadworld.com/u/dquimby)\
**Post date:** [September 6, 2022, 12:56pm UTC](https://forums.toadworld.com/t/create-relationship-with-script-perrelation-changekey/55977/3 "2022-09-06T12:56:54Z")

</div>

Thanks Petr, that worked perfectly.
