TMVCActiveRecord // in WriteableFieldsCount is considered also a not-autogenerated primary key

This commit is contained in:
Daniele Teti 2024-11-05 12:24:45 +01:00
parent 2e706f483f
commit 1bb4d7f7ca

View File

@ -278,6 +278,7 @@ type
fDefaultRQLFilter: string; fDefaultRQLFilter: string;
fMap: TFieldsMap; fMap: TFieldsMap;
fPrimaryKey: TRTTIField; fPrimaryKey: TRTTIField;
fPrimaryKeyInInsert: Boolean;
fMapping: TMVCFieldsMapping; fMapping: TMVCFieldsMapping;
fPropsAttributes: TArray<TCustomAttribute>; fPropsAttributes: TArray<TCustomAttribute>;
fProps: TArray<TRTTIField>; fProps: TArray<TRTTIField>;
@ -1590,6 +1591,7 @@ begin
begin begin
if foPrimaryKey in MVCTableFieldAttribute(lAttribute).FieldOptions then if foPrimaryKey in MVCTableFieldAttribute(lAttribute).FieldOptions then
begin begin
lTableMap.fPrimaryKeyInInsert := not (foAutoGenerated in MVCTableFieldAttribute(lAttribute).FieldOptions);
lTableMap.fPrimaryKey := lRTTIField; lTableMap.fPrimaryKey := lRTTIField;
lPrimaryFieldTypeAsStr := lTableMap.fPrimaryKey.FieldType.ToString.ToLowerInvariant; lPrimaryFieldTypeAsStr := lTableMap.fPrimaryKey.FieldType.ToString.ToLowerInvariant;
if lPrimaryFieldTypeAsStr.EndsWith('int64') then if lPrimaryFieldTypeAsStr.EndsWith('int64') then
@ -1678,13 +1680,13 @@ begin
OnValidation(TMVCEntityAction.eaCreate); OnValidation(TMVCEntityAction.eaCreate);
OnBeforeInsert; OnBeforeInsert;
OnBeforeInsertOrUpdate; OnBeforeInsertOrUpdate;
if fTableMap.fMap.WritableFieldsCount = 0 then if (fTableMap.fMap.WritableFieldsCount = 0) and (not fTableMap.fPrimaryKeyInInsert) then
begin begin
raise EMVCActiveRecord.CreateFmt raise EMVCActiveRecord.CreateFmt
('Cannot insert an entity if no fields are writable. Class [%s] mapped on table [%s]', ('Cannot insert an entity if no fields are writable. Class [%s] mapped on table [%s]',
[ClassName, TableName]); [ClassName, TableName]);
end; end;
if (foAutoGenerated in fTableMap.fPrimaryKeyOptions) then if (not fTableMap.fPrimaryKeyInInsert) {autogenerated} then
begin begin
if not SQLGenerator.HasReturning then if not SQLGenerator.HasReturning then
begin begin