@@ -1153,8 +1153,12 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
1153
1153
1154
1154
bool returnvalue = true ;
1155
1155
1156
+ QMap< QString, QgsField > mapFieldNameToOriginalField;
1157
+
1156
1158
for ( QList<QgsField>::const_iterator iter = attributes.begin (); iter != attributes.end (); ++iter )
1157
1159
{
1160
+ mapFieldNameToOriginalField[ iter->name ()] = *iter;
1161
+
1158
1162
OGRFieldType type;
1159
1163
1160
1164
switch ( iter->type () )
@@ -1164,8 +1168,16 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
1164
1168
break ;
1165
1169
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000
1166
1170
case QVariant::LongLong:
1167
- type = OFTInteger64;
1171
+ {
1172
+ const char * pszDataTypes = GDALGetMetadataItem ( ogrDriver, GDAL_DMD_CREATIONFIELDDATATYPES, NULL );
1173
+ if ( pszDataTypes && strstr ( pszDataTypes, " Integer64" ) )
1174
+ type = OFTInteger64;
1175
+ else
1176
+ {
1177
+ type = OFTReal;
1178
+ }
1168
1179
break ;
1180
+ }
1169
1181
#endif
1170
1182
case QVariant::Double:
1171
1183
type = OFTReal;
@@ -1203,6 +1215,26 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
1203
1215
OGR_Fld_Destroy ( fielddefn );
1204
1216
}
1205
1217
loadFields ();
1218
+
1219
+ // The check in QgsVectorLayerEditBuffer::commitChanges() is questionable with
1220
+ // real-world drivers that might only be able to satisfy request only partially.
1221
+ // So to avoid erroring out, patch field type, width and precision to match
1222
+ // what was requested.
1223
+ // For example in case of Integer64->Real mapping so that QVariant::LongLong is
1224
+ // still returned to the caller
1225
+ // Or if a field width was specified but not strictly enforced by the driver (#15614)
1226
+ for ( QMap< QString, QgsField >::const_iterator it = mapFieldNameToOriginalField.begin ();
1227
+ it != mapFieldNameToOriginalField.end (); ++it )
1228
+ {
1229
+ int idx = mAttributeFields .fieldNameIndex ( it.key () );
1230
+ if ( idx >= 0 )
1231
+ {
1232
+ mAttributeFields [ idx ].setType ( it->type () );
1233
+ mAttributeFields [ idx ].setLength ( it->length () );
1234
+ mAttributeFields [ idx ].setPrecision ( it->precision () );
1235
+ }
1236
+ }
1237
+
1206
1238
return returnvalue;
1207
1239
}
1208
1240
0 commit comments