@@ -384,20 +384,12 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons
384
384
if ( attributes.contains ( idx ) )
385
385
{
386
386
v = attributes.value ( idx );
387
- if ( fields.at ( idx ).constraints ().constraints () & QgsFieldConstraints::ConstraintUnique
388
- && QgsVectorLayerUtils::valueExists ( layer, idx, v ) )
389
- {
390
- // unique constraint violated
391
- QVariant uniqueValue = QgsVectorLayerUtils::createUniqueValue ( layer, idx, v );
392
- if ( uniqueValue.isValid () )
393
- v = uniqueValue;
394
- }
395
- checkUnique = false ;
396
387
}
397
388
398
389
// 2. client side default expression
399
390
// note - deliberately not using else if!
400
- if ( !v.isValid () && layer->defaultValueDefinition ( idx ).isValid () )
391
+ if ( ( !v.isValid () || ( fields.at ( idx ).constraints ().constraints () & QgsFieldConstraints::ConstraintUnique && QgsVectorLayerUtils::valueExists ( layer, idx, v ) ) )
392
+ && layer->defaultValueDefinition ( idx ).isValid () )
401
393
{
402
394
// client side default expression set - takes precedence over all. Why? Well, this is the only default
403
395
// which QGIS users have control over, so we assume that they're deliberately overriding any
@@ -407,7 +399,8 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons
407
399
408
400
// 3. provider side default value clause
409
401
// note - not an else if deliberately. Users may return null from a default value expression to fallback to provider defaults
410
- if ( !v.isValid () && fields.fieldOrigin ( idx ) == QgsFields::OriginProvider )
402
+ if ( ( !v.isValid () || ( fields.at ( idx ).constraints ().constraints () & QgsFieldConstraints::ConstraintUnique && QgsVectorLayerUtils::valueExists ( layer, idx, v ) ) )
403
+ && fields.fieldOrigin ( idx ) == QgsFields::OriginProvider )
411
404
{
412
405
int providerIndex = fields.fieldOriginIndex ( idx );
413
406
QString providerDefault = layer->dataProvider ()->defaultValueClause ( providerIndex );
@@ -420,7 +413,8 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons
420
413
421
414
// 4. provider side default literal
422
415
// note - deliberately not using else if!
423
- if ( !v.isValid () && fields.fieldOrigin ( idx ) == QgsFields::OriginProvider )
416
+ if ( ( !v.isValid () || ( fields.at ( idx ).constraints ().constraints () & QgsFieldConstraints::ConstraintUnique && QgsVectorLayerUtils::valueExists ( layer, idx, v ) ) )
417
+ && fields.fieldOrigin ( idx ) == QgsFields::OriginProvider )
424
418
{
425
419
int providerIndex = fields.fieldOriginIndex ( idx );
426
420
v = layer->dataProvider ()->defaultValue ( providerIndex );
@@ -431,6 +425,13 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons
431
425
}
432
426
}
433
427
428
+ // 5. passed attribute value
429
+ // note - deliberately not using else if!
430
+ if ( !v.isValid () && attributes.contains ( idx ) )
431
+ {
432
+ v = attributes.value ( idx );
433
+ }
434
+
434
435
// last of all... check that unique constraints are respected
435
436
// we can't handle not null or expression constraints here, since there's no way to pick a sensible
436
437
// value if the constraint is violated
0 commit comments