Is there are way to use the TFDTable.CreateTable method in Delphi to automatically create the trigger and generator for an autoinc field?
Using this as an example
Create table with firedac without SQL Script
I have tried the following:
Table := TFDTable.Create(nil);
try
Table.Connection := MyConnection;
Table.TableName := 'Vehicles';
Table.FieldDefs.Add('VehIdx', ftAutoInc, 0, True);
Table.FieldDefs.Add('PIN', ftString, 15, False);
Table.FieldDefs.Add('VehID', ftString, 15, False);
Table.FieldDefs.Add('VehName', ftString, 50, False);
Table.AddIndex('pkVehiclesIdx', 'VehIdx', '', [soPrimary]);
Table.CreateTable(True);
finally
Table.Free;
end;
But I get "Generator GEN_VEHICLES not found".
I'm using Firebird.
I know that I can create the trigger and generator myself. But it seems that FireDAC should be able to do it. I'm wondering how to make it do that?
OK, I have found out how to do it. Here's the complete code:
procedure CreateVehiclesTable;
var
Table: TFDTable;
begin
Table := TFDTable.Create(nil);
try
Table.Connection := FrmMain.ConLocal;
Table.TableName := 'Vehicles';
Table.FieldDefs.Add('VehIdx', ftAutoInc, 0, False);
Table.FieldDefs.Add('PIN', ftString, 15, False);
Table.FieldDefs.Add('VehID', ftString, 15, False);
Table.FieldDefs.Add('VehName', ftString, 50, False);
Table.OptionsIntf.UpdateOptions.GeneratorName := 'GEN_VEHICLES';
Table.AddIndex('pkVehiclesIdx', 'VehIdx', '', [soPrimary]);
Table.CreateTable(False, [tpTable, tpPrimaryKey, tpGenerators, tpTriggers]);
finally
Table.Free;
end;
Manually setting the generator name makes everything work correctly. The trigger and generator are created and the VEHIDX field increments automatically as it should.