(I complete by an example, the usefulness of knowing that we can play, with the order in which the components are created)I take an example of a master-details relationship composed with 2 IBX components TIBDataset for Firebird (using a datasource property), to explain this use:
I have a Firebird table TRIGGERS whose schema is:
TRIGGER_PER_DIRECTORY_ID, -- primary key
PATH_CLIENT_TRIGGER,
PARAMETERS,
IS_ACTIVE,
DIRECTORY_ID -- foreign key
So, assuming that the order of creation of the dataset's TFields collection -
what is called the TFields definition in Lazarus or the TFieldDefs in Delphi - follows the same order as that displayed in Firebird \ FlameRobin \ IBeasy \... I wrote:
dtsTriggersDetailsByDir_rw.AppendRecord([nil, edtDir.text, ' ', 0, dtsDirsMaster_rw.FieldByName('DIRECTORY_ID').AsInteger]);
==> I get a laconic message
" ' ' " is not a valid integer" (
btw, these too succinct messages will be more complete in a future version of Laz\FPC: they will indicate the name of the field in question). The conclusion is therefore, that I pass an order of values - to append - which does not follow - in fact - the order of the original schema of the physical SQL table. Which was my presupposition.
To get around this problem, there are 2 solutions:- do not rely on the TFields collection dynamically created by Lazarus at each dataset creation, but create your own TFields collection of persistent dataset fields and order them in the order you want to immutable, i.e. this fields collection's order will be respected at each re-creation of the dataset-TIBdataset (typically: the persistent order chosen could be the same as the order of the fields, such as it is in the physical SQL table; Cf. the screen shots).
- use the Append method and sets each value in its named field:
with dtsTriggersDetailsByDir_rw do begin
Append;
FieldByName('PATH_CLIENT_TRIGGER').AsString:= edtPathClientTrigger.Text;
FieldByName('PARAMETERS').AsString:= ' ';
FieldByName('IS_ACTIVE').AsString:= 0;
FieldByName('DIRECTORY_ID').AsInteger:= dtsDirsMaster_rw.FieldByName('DIRECTORY_ID').AsInteger;
Post;
end;
nb: the TRIGGER_PER_DIRECTORY_ID field - the primary key - is affected by its generator.
➔ This will work all the time, without the need to create an ordered TFields collection of persistent fields, if one is not intransigent about an imperative existence of a personal order with the TFields collection of the dataset (also called TFields definition).