Hi,
TDM version : 6.0
I am writing a macro to enforce naming conventions. I could successfully implement it for Primary keys, alternate keys, unique keys etc. I could also implement it for Relationships, however, I am stuck on how to find the column name of the parent table it is referring to.
Let me show an example of Emp-Dept tables to explain clearly. Let’s say I have the following relationship for which I am checking the naming convention as “dept_deptno_emp_deptno_fk”
Example :
ALTER TABLE “scott”.“dept” ADD CONSTRAINT “dept_deptno_emp_deptno_fk” FOREIGN KEY (“deptno”) REFERENCES “scott”.“emp” (“deptno”);
The foreign key and the column name it references are same “deptno”. But let’s say the names are different, in such case I need the foreign key as well as the column name it is referring to, since the naming convention rule for relationships I need is:
ChildEntity_ForignKey_ParentEntity_ParentKey_FK
Macro:
for (i = 0; i < model.Relations.Count; i++)
{
obj = model.Relations.GetObject(i);
relationship = null;
if (obj.ObjectType == 2504) // relation line selected on
{
relationship = obj.ParentBase;
}
else if (obj.ObjectType == 2004) // relation selected
// in Model Explorer
// or Object Viewer
{
relationship = obj;
}
if (relationship != null)
{
//System.ShowMessageDialog(++dialogId, ‘InfoDialog’, "rel name is " + relationship.Name, 2, 4);
for (fk = 0; fk < relationship.ForeignKeys.Count; fk++)
{
constraint = relationship.ForeignKeys.GetObject(fk).KeyConstraintItem;
//System.ShowMessageDialog(++dialogId, ‘InfoDialog’, constraint.Name, 2, 4);
}
//System.ShowMessageDialog(++dialogId, ‘InfoDialog’, "the fkey index name is " + relationship.ForeignIndex.Name, 2, 4);
name = relationship.ChildEntity.Name + “" + constraint.Name + "” + relationship.ParentEntity.Name + “_” + + “_fk”;
//System.ShowMessageDialog(++dialogId, ‘InfoDialog’, "new rel name is " + name, 2, 4);
relationship.Name = name;
relationship.Caption = name;
}
}
Question:
Relationship name : ChildEntity_ForignKey_ParentEntity_ParentKey_FK
For above relationship name, how to get the ParentKey i.e. the column in the parent table it is referring to?
Please let me know if you need more information.
Regards,
Lalit