Aurelius - deleting items from a object

Hello,
I need help deleting items from a object

There is my database structure :

CREATE TABLE IF NOT EXISTS headers (
ID int(11) NOT NULL AUTO_INCREMENT,
code varchar(50) NOT NULL,
name varchar(100) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS items (
id int(11) NOT NULL AUTO_INCREMENT,
parent_id int(11) NOT NULL,
posindex int(11) NOT NULL,
note varchar(100) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

model is in other file model.pas

First i will create an object :
var
mgr : TAureliusManager;
mh : THeaders;
begin
mgr := TAureliusManager.Create(self);
Try
mgr.Connection := AureliusConnection1;
mh := THeaders.Create;
mh.Code := 'tst';
mh.Name := 'tst no rows';
mgr.Save(mh);
Finally
mgr.Free;
End;
end;

it is correct

second i will insert to rows/items
var
mgr : TAureliusManager;
mh : THeaders;
mr : TItems;
begin
mgr := TAureliusManager.Create(self);
Try
mgr.Connection := AureliusConnection1;
mh := mgr.Find(1);
mh.Name := 'tst added Rows';
mr := TItems.Create;
mr.posindex := 1;
mr.note := 'Row number 1';
mh.ItemsList.Add(mr);
mr.parent_id := mh;
mr := TItems.Create;
mr.posindex := 2;
mr.note := 'Row number 2';
mh.ItemsList.Add(mr);
mr.parent_id := mh;
mgr.Flush;
Finally
mgr.Free;
End;
end;

it is corrent too

Finally, I want to delete one of the rows so that it disappears from the database with the flush command.
var
mgr : TAureliusManager;
mh : THeaders;
mr : TItems;
begin
mgr := TAureliusManager.Create(self);
Try
mgr.Connection := AureliusConnection1;
mh := mgr.Find(1);
mh.Name := 'One Row Deleted';
If mh.ItemsList.Count = 0 Then
raise Exception.Create('There is not any rows');
mr := mh.ItemsList.Items[0];
// QUESTION how to delete a row so that it is physically removed from the db even during flush?
mgr.Flush;
Finally
mgr.Free;
End;

mgr.Remove(mr);

1 Like

Alternatively to the correct answer from @Ferrari_Julio, you can always do this:

mh.ItemsList.Delete(0);
mgr.Flush;

IF you you add TCascadeType.RemoveOrphan to the THeader.FItems many-valued association. References: