You are not logged in.
Hi everyone,
It's not about mORMot2, but i am using latest version of mORMot and find a small bug imo.
function TSQLRest.AddOrUpdate(Value: TSQLRecord; ForceID: boolean): TID;
begin
if (self=nil) or (Value=nil) then begin
result := 0;
exit;
end;
if ForceID or (Value.fID=0) then begin // Add, raises an exception
result := Add(Value,true,ForceID);
if (result<>0) or (Value.fID=0) then
exit;
end;
if Update(Value) then
result := Value.fID else
result := 0;
end;
When i set ForceID to True, AddOrUpdate raises an exception because it's uses OR. In this scenario even Value.fID is not 0 it still try to add this record. Isn't this suppose to be AND operator?
Last edited by koraycayiroglu (2021-12-28 14:20:01)
Offline
ForceID is expected to be set with ID<>0.
It is documented as such.
Please read the comment/documentation of the method definition:
// - implements REST POST if Value.ID=0 or ForceID is set, or a REST PUT
// collection to update the record pointed by a Value.ID<>0
Online
Sorry, i thought AddOrUpdate means as it is and when i set ForceID to true, it will still add or update the same record with same ID. No problems, i changed it as:
function TSQLRest.AddOrUpdate(Value: TSQLRecord; ForceID: boolean): TID;
begin
if (self=nil) or (Value=nil) then begin
result := 0;
exit;
end;
if (Value.fID=0) or (ForceID and (not MemberExists(Value.RecordClass, Value.fID))) then begin
result := Add(Value,true,ForceID);
if (result<>0) or (Value.fID=0) then
exit;
end;
if Update(Value) then
result := Value.fID else
result := 0;
end;
it's better this way imo.
Last edited by koraycayiroglu (2021-12-29 10:50:42)
Offline