Problem with RemoveOrphan - EReferenceNotFound

Hello!

I have a problem using remove orphan functionality of Aurelius. Here are the classes and DB definitions:

  [Entity]
  [Table('Partner')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  TPartner = class
  private
    [Column('Id', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FId: Integer;
    
    [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAllRemoveOrphan, 'FPartner')]
    FMemberOf: Proxy<TList<TPartnerGroup>>;    
  public
    property Id: Integer read FId write FId;
    property MemberOf: TList<TPartnerGroup> read GetMemberOf;
  end;

  [Entity]
  [Table('PartnerGroup')]
  [Id('FId', TIdGenerator.IdentityOrSequence)]
  TPartnerGroup = class
  private
    [Column('Id', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FId: Integer;
    
    [Association([TAssociationProp.Lazy], CascadeTypeAll)]
    [JoinColumn('InGroup', [], 'Id')]
    FInGroup: Proxy<TTypePartner>;
    
    [Association([TAssociationProp.Lazy], CascadeTypeAll)]
    [JoinColumn('Partner', [], 'Id')]
    FPartner: Proxy<TPartner>;
      public
    property Id: Integer read FId write FId;
    property InGroup: TTypePartner read GetInGroup write SetInGroup;
    property Partner: TPartner read GetPartner write SetPartner;
  end;

MySQL table definitions:

CREATE TABLE `partner` (
	`Id` INT(11) NOT NULL AUTO_INCREMENT,
	`Name` VARCHAR(250) NOT NULL COLLATE 'cp1250_croatian_ci',
	PRIMARY KEY (`Id`) USING BTREE,
	INDEX `TypeComm_Partners` (`CommType`) USING BTREE,
	INDEX `User_Partners` (`UserOwner`) USING BTREE,
	INDEX `Zip_Partners` (`Zip`) USING BTREE
);


CREATE TABLE `partnergroup` (
	`Id` INT(11) NOT NULL AUTO_INCREMENT,
	`Partner` INT(11) NULL DEFAULT NULL,
	`InGroup` INT(11) NULL DEFAULT NULL,
	PRIMARY KEY (`Id`) USING BTREE,
	INDEX `TypePartner_PartnerGroup` (`InGroup`) USING BTREE,
	INDEX `Partners_PartnerGroup` (`Partner`) USING BTREE,
	CONSTRAINT `Partners_PartnerGroup` FOREIGN KEY (`Partner`) REFERENCES `partner` (`Id`) ON UPDATE RESTRICT ON DELETE CASCADE,
	CONSTRAINT `TypePartner_PartnerGroup` FOREIGN KEY (`InGroup`) REFERENCES `typepartner` (`Id`) ON UPDATE RESTRICT ON DELETE CASCADE
);

I use the classes via an XData service:

function TEasyPartnerService.SavePartner(Partner: TPartner): integer;
begin
    if Partner.Id < 1 then begin
      mngr.SaveOrUpdate(Partner);
      mngr.Flush;
    end else begin
      mngr.Flush(mngr.Merge<TPartner>(Partner));
    end;
end;

When removing an item from the list (on client side) and calling the service, I get this error:

EReferenceNotFound with message 'Reference "PartnerGroup(8)/Partner" not found'.

If I run tis SQL, then I get the correct data in theDB (the partner from which I removed the TPartnerType.

SELECT A.Id AS A_Id, A.InGroup AS A_InGroup, A.Partner AS A_Partner
FROM PartnerGroup A
WHERE  A.Id = 8

I cannot find the origin of the problem. I'm quite lost and any help / tip would be appreciated.

Kind regards!

Can you please create a minimal project with client and server, reproducing the issue, so we can debug and check what's wrong?

I will first try to recreate the DB to be sure that is up-to-date with the Aurelius objects. Will report here next week.

1 Like