danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Active Record (ORM) does not allow type guid as primary key

marcelojaloto opened this issue · comments

When you have a primary key field as uuid in the Postgres SQL, the DMVC framework feature Active Record raise the exception: "Allowed primary key types are: (Nullable)Integer, (Nullable)Int64, (Nullable)String - found: guid".

This occurs at the line 1182 of the unit MVCFramework.ActiveRecord in the method InitTableInfo.

My sollution include de lines below in the method InitTableInfo:

else if lPrimaryFieldTypeAsStr.EndsWith('guid') then
begin
    fPrimaryKeyFieldType := ftGuid;
end

Can i to send a Pull request with this solution? Or exist other solution for this case?

Thanks,
Marcelo Jaloto

This is the intended behaviour. If you can run all the unit tests with your implementation feel free to provide a PR

Ok, i do it.

Thanks,
Marcelo Jaloto

Hi @danieleteti

I finished my tests and added new improvements for use with guid-type primary key fields.

I will send my solutions in new pull request for you. Thanks!

  • Fixes bug when a Active Record field primary key is a Guid;
else if lPrimaryFieldTypeAsStr.EndsWith('guid') then
begin
    fPrimaryKeyFieldType := ftGuid;
end
  • Adds new custom attribute, MVCSerializeGuidWithoutBracesAttribute, in the model class to define a field of type TGuid if at the time of attribute serialization the value of the guid field will be obtained without braces;
    [MVCSerializeGuidWithoutBraces]
    [MVCSwagJSONSchemaField(stGuid, 'id', 'User Id', True)]
    property Id: TGuid read GetKey write SetKey;
  • New improvements for use with fields of primary key of kind Guid;
class function TMVCActiveRecord.GetByPK(const aClass: TMVCActiveRecordClass; const aValue: TGuid;
  const RaiseExceptionIfNotFound: Boolean): TMVCActiveRecord;

function TMVCActiveRecord.LoadByPK(const id: TGuid): Boolean;

Important notes:

https://quality.embarcadero.com/browse/RSP-19755?jql=text%20~%20%22firedac%20guid%22

image

Samples:

in TFDCustomManager

var Def := TFDPhysPGConnectionDefParams(FDManager.ConnectionDefs.ConnectionDefByName('connection-name').Params);
Def.GUIDEndian := Big;

or in TFDConnection

(TFDConnection.Params as TFDPhysPGConnectionDefParams).GUIDEndian := Big;

Thanks Marcelo. The changes has been merged, extended, integrated and fully tested.

You are welcome.
@danieleteti thanks for your attention.
Congratulations for fully tests.
See you soon!