Toad World® Forums

Script for Relation Rename


#1

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.’);
}


#2

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


#3

Vladka,
No problem.
Thank you,
Andrei


#4

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”);
}


#5

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