# Script for Relation Rename

**URL:** <https://forums.toadworld.com/t/script-for-relation-rename/2182>\
**Category:** Toad Data Modeler\
**Created:** [January 7, 2010, 8:26pm UTC](https://forums.toadworld.com/t/script-for-relation-rename/2182 "2010-01-07T20:26:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![agsergeev\_890](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@agsergeev\_890](https://forums.toadworld.com/u/agsergeev_890)\
**Post date:** [January 7, 2010, 8:26pm UTC](https://forums.toadworld.com/t/script-for-relation-rename/2182/1 "2010-01-07T20:26:52Z")

</div>

Vladka,  
I have a script that I was using with v.2 for renaming relations. Unfortunately I’m a little bit lost in your new object model for v.3. I just need to rename relations and don’t want to spend time to learn v.3 Objects. Could you help me please?  
Thanks,  
Andrei

// This script renames all relation names and foreing key constraints (replace them by names of parent and child entities)

function main(){

```
var app = System.GetInterface('Application');
var Model = app.Models.GetObject(0); // first model in Application View
var Log = System.CreateObject('Log');

//Log.Clear();
//Model.ReadOnly = false;
Model.Lock();
for (r=0; r<Model.Relations.Count; r++)
{
    Relation = Model.Relations(r);
    ParentEntity = Model.GetEntity(Relation.ParentEntityId);
    ChildEntity = Model.GetEntity(Relation.ChildEntityId);

    // logical names
    OldLogicalName = Relation.Name;
    ParentName = ParentEntity.Name;
    ChildName = ChildEntity.Name;
    Relation.Name = ParentName+'_'+ChildName;

    // physical names
    OldPhysicalName = Relation.FKConstraint;
    ParentName = ParentEntity.TableName;
    ChildName = ChildEntity.TableName;
    Relation.FKConstraint = ParentName+'_'+ChildName;

    //Log.Writeln('Relation '+OldLogicalName+' ('+OldPhysicalName+') renamed to '+Relation.Name+' ('+Relation.FKConstraint+')');
    Log.Information('Relation '+OldLogicalName+' ('+OldPhysicalName+') renamed to '+Relation.Name+' ('+Relation.FKConstraint+')');
}
//Model.ReadOnly = true;

```

Model.UnLock();  
Alert(‘All relations renamed.’);  
}

---

<div class="post-metadata">

**Author:** ![vladka](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/vladka/32/41_2.png) [@vladka](https://forums.toadworld.com/u/vladka)\
**Post date:** [January 9, 2010, 2:33pm UTC](https://forums.toadworld.com/t/script-for-relation-rename/2182/2 "2010-01-09T14:33:15Z")

</div>

Hello Andrei,

Our script expert Mario will check it out. I’ll get back to you on Monday or Tuesday.  
Thanks for your patience.

Regards,

Vladka

---

<div class="post-metadata">

**Author:** ![agsergeev\_890](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@agsergeev\_890](https://forums.toadworld.com/u/agsergeev_890)\
**Post date:** [January 11, 2010, 12:19pm UTC](https://forums.toadworld.com/t/script-for-relation-rename/2182/3 "2010-01-11T12:19:46Z")

</div>

Vladka,  
No problem.  
Thank you,  
Andrei

---

<div class="post-metadata">

**Author:** ![vladka](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/vladka/32/41_2.png) [@vladka](https://forums.toadworld.com/u/vladka)\
**Post date:** [January 12, 2010, 1:44am UTC](https://forums.toadworld.com/t/script-for-relation-rename/2182/4 "2010-01-12T01:44:17Z")

</div>

Hi Andrei,

Here’s an example script for renaming relationship names.  
It’s O.K. when only Caption (logical name) is changed in the script as by default Name changes with the Caption.

If you have any questions, please write us back. Thanks.

Regards,

Vladka + Mario

===========  
function main()  
{  
var app = System.GetInterface(‘Application’);  
var Model = app.Models.GetObject(0); // first model in Application View  
var r;  
var Relation, ParentEntity, ChildEntity;  
var OldCaption, ParentCaption, ChildCaption;

Model.Lock();  
for( r=0; r\<Model.Relations.Count; r++ )  
{  
Relation = Model.Relations.GetObject®;  
Relation.Lock();

ParentEntity = Relation.ParentEntity;  
ChildEntity = Relation.ChildEntity;

```
// caption
OldCaption = Relation.Caption;
ParentCaption = ParentEntity.Caption;
ChildCaption = ChildEntity.Caption;
Relation.Caption = ParentCaption+'_'+ChildCaption;

```

Relation.UnLock();

Log.Information('Relation ‘+OldCaption+’ renamed to '+Relation.Caption);  
}  
Model.UnLock();

Log.Information(“DONE”);  
}

---

<div class="post-metadata">

**Author:** ![agsergeev\_890](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@agsergeev\_890](https://forums.toadworld.com/u/agsergeev_890)\
**Post date:** [January 14, 2010, 5:38pm UTC](https://forums.toadworld.com/t/script-for-relation-rename/2182/5 "2010-01-14T17:38:42Z")

</div>

Vladka and Mario,  
Thank you very much. It wroks.  
Andrei.
