#1 2023-11-14 06:29:23

sgavrilov
Member
Registered: 2019-05-24
Posts: 20

TClassNameToBeDeletedID does not work

Hello,

Environment: Delphi XE2, TRestServerDB, SQLite3 database, 4 tables in a model, one of the tables references another by a TClassNameToBeDeletedID field.

It appears that the TClassNameToBeDeletedID does not work as expected: the field index is not created and (I suspect) the cascade deletion does not work either.

I have found the immediate cause for this in the unit mormot.orm.core but deeper details elude me.

constructor TOrmPropInfoRttiTID.Create(aPropInfo: PRttiProp; aPropIndex: integer; aOrmFieldType: TOrmFieldType; aOptions: TOrmPropInfoListOptions);
...

  if IdemPropName(TypeName^, 'TID') or
     (ord(TypeName^[1]) and $df <> ord('T')) or // expect T...ID pattern
     (PWord(@TypeName^[L - 1])^ and $dfdf <> ord('I') + ord('D') shl 8) or
     (Rtti.Counts[rkClass] = 0) then
    exit;
  if (L > 13) and IdemPropName('ToBeDeletedID', @TypeName^[L - 12], 13) then
  begin   // 'TOrmClientToBeDeletedID' -> TOrmClient + CascadeDelete=true
    fCascadeDelete := true;
    Found := Rtti.FindName(@TypeName^[1], L - 13, rkClass);
  end
...
end;

The condition (Rtti.Counts[rkClass] = 0) is False. If it is commented out then the code works as expected.

Best regards,
Sergey

Offline

#2 2023-11-28 09:21:20

sgavrilov
Member
Registered: 2019-05-24
Posts: 20

Re: TClassNameToBeDeletedID does not work

It appears that I have found the problem: it is in the unit mormot.core.rtti.

It seems to me that instead of

inc(Counts[Instance.Kind]);

the following code should be used in the procedure TRttiCustomList.AddToPairs(Instance: TRttiCustom; Info: PRttiInfo):

inc(Counts[Info.Kind]);

When the AddToPairs is called from the function TRttiCustomList.DoRegister

    // initialize a new TRttiCustom/TRttiJson instance for this type
    result := GlobalClass.Create;
    // register ASAP to avoid endless recursion in FromRtti
    AddToPairs(result, Info);
    // now we can parse and process the RTTI
    result.FromRtti(Info);

the first parameter (result) is not completely initialized yet and the Instance.Kind always equals to rkUnknown inside the AddToPairs. Therefore, the correct counters in the array are not incremented.

Offline

#3 2023-11-28 10:46:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: TClassNameToBeDeletedID does not work

Offline

#4 2023-11-28 11:02:15

sgavrilov
Member
Registered: 2019-05-24
Posts: 20

Re: TClassNameToBeDeletedID does not work

Thank you!

Offline

Board footer

Powered by FluxBB