Create Index Using Data Modeler script

Hi,
I'm trying to add a "Non Exclusive" DBIndex on a table that already as Unique Index(UK_C00461CD_ACCDEV),
but when export DataModeler To Aurelius, export add another "Non Exclusive" Index to the table.

procedure OnClassGenerated(Args: TClassGeneratedArgs);
var
I, J: Integer;
Idx: TGDAOIndex;
Fields: string;
Attr: TCodeAttributeDeclaration;
begin
for I := 0 to Args.DBTable.Indexes.Count - 1 do
begin

if Args.DBTable.Indexes[I].Items.IndexType = TIndexType.NonExclusive then //How to check NonExclusive Index???
begin
Idx := Args.DBTable.Indexes[I];
Fields := '';
for J := 0 to Idx.IFields.Count - 1 do
begin
if Fields <> '' then Fields := Fields + ',';
Fields := Fields + Idx.IFields[J].FieldName;
end;
Attr := Args.CodeType.AddAttribute('DBIndex');
Attr.AddRawArgument('''' + Idx.IndexName + '''');
Attr.AddRawArgument('''' + Fields + '''');
end;

end;
end;

Another issue, i have a very tricky way to add Unique Index to existing table, is there a building in solution ?
Thanks, João

Hi there

    if (Idx.IndexType=0) and (Idx.IFields.Count>0) then  

HTH

1 Like

Hi

if (Idx.IndexType=0) and (Idx.IFields.Count>0) then

that's it, Thanks

1 Like

What is your issue with UniqueIndex?

Hi, when Updatedatabase this is the issue:
"Error(cannot be executed, do it manually): Unique key: AccBank.UK_CF5C7C98_AccBank - Created"

to avoid that, I have to execute:
ALTER TABLE AccBank ADD Constraint UK_C9778EB8_AccBank UNIQUE (IBAN), on database level before run UpdateDatabase on Aurelius.

When create new database, Datamodeler ignores the name that i chose 'UK_IBAN_AccBank' and
create UK_C9778EB8_AccBank.

So,... when i need to add a new Unique index on existing table, i have to execute UpdateDatabase, inspect log file error to
check index name "UK_C9778EB8_AccBank", and finally i can execute ALTER TABLE manually

Thanks

Ok, that's the expected behavior. If you want Aurelius to ignore the constraint names (both unique key and foreign key) when comparing database structures, you can set the property IgnoreConstraintName to True in the TDatabaseManager object you are using to update the database:

DBManager.IgnoreConstraintName := True;
DBManager.UpdateDatabase;

Ok, thanks Wagner