Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
marcelojaloto opened this issue May 9, 2022 · 6 comments
Closed

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

marcelojaloto opened this issue May 9, 2022 · 6 comments
Assignees
Labels
as-designed The behaviour is correct/expected enhancement

Comments

@marcelojaloto
Copy link
Contributor

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

@danieleteti
Copy link
Owner

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

@danieleteti danieleteti added enhancement as-designed The behaviour is correct/expected labels May 9, 2022
@marcelojaloto
Copy link
Contributor Author

Ok, i do it.

Thanks,
Marcelo Jaloto

@marcelojaloto
Copy link
Contributor Author

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;

@marcelojaloto
Copy link
Contributor Author

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;

@danieleteti
Copy link
Owner

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

@danieleteti danieleteti added this to the 3.2.2-nitrogen milestone Jun 23, 2022
@danieleteti danieleteti self-assigned this Jun 23, 2022
@marcelojaloto
Copy link
Contributor Author

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

marcelojaloto added a commit to marcelojaloto/delphimvcframework that referenced this issue Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as-designed The behaviour is correct/expected enhancement
Projects
None yet
Development

No branches or pull requests

2 participants