|
@@ -380,17 +380,32 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons |
|
|
bool checkUnique = true; |
|
|
|
|
|
// in order of priority: |
|
|
// 1. passed attribute value and if field does not have a unique constraint like primary key |
|
|
if ( attributes.contains( idx ) ) |
|
|
{ |
|
|
v = attributes.value( idx ); |
|
|
if ( fields.at( idx ).constraints().constraints() & QgsFieldConstraints::ConstraintUnique |
|
|
&& QgsVectorLayerUtils::valueExists( layer, idx, v ) ) |
|
|
{ |
|
|
// unique constraint violated |
|
|
QVariant uniqueValue = QgsVectorLayerUtils::createUniqueValue( layer, idx, v ); |
|
|
if ( uniqueValue.isValid() ) |
|
|
v = uniqueValue; |
|
|
} |
|
|
checkUnique = false; |
|
|
} |
|
|
|
|
|
// 1. client side default expression |
|
|
if ( layer->defaultValueDefinition( idx ).isValid() ) |
|
|
// 2. client side default expression |
|
|
// note - deliberately not using else if! |
|
|
if ( !v.isValid() && layer->defaultValueDefinition( idx ).isValid() ) |
|
|
{ |
|
|
// client side default expression set - takes precedence over all. Why? Well, this is the only default |
|
|
// which QGIS users have control over, so we assume that they're deliberately overriding any |
|
|
// provider defaults for some good reason and we should respect that |
|
|
v = layer->defaultValue( idx, newFeature, evalContext ); |
|
|
} |
|
|
|
|
|
// 2. provider side default value clause |
|
|
// 3. provider side default value clause |
|
|
// note - not an else if deliberately. Users may return null from a default value expression to fallback to provider defaults |
|
|
if ( !v.isValid() && fields.fieldOrigin( idx ) == QgsFields::OriginProvider ) |
|
|
{ |
|
@@ -403,7 +418,7 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons |
|
|
} |
|
|
} |
|
|
|
|
|
// 3. provider side default literal |
|
|
// 4. provider side default literal |
|
|
// note - deliberately not using else if! |
|
|
if ( !v.isValid() && fields.fieldOrigin( idx ) == QgsFields::OriginProvider ) |
|
|
{ |
|
@@ -416,13 +431,6 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons |
|
|
} |
|
|
} |
|
|
|
|
|
// 4. passed attribute value |
|
|
// note - deliberately not using else if! |
|
|
if ( !v.isValid() && attributes.contains( idx ) ) |
|
|
{ |
|
|
v = attributes.value( idx ); |
|
|
} |
|
|
|
|
|
// last of all... check that unique constraints are respected |
|
|
// we can't handle not null or expression constraints here, since there's no way to pick a sensible |
|
|
// value if the constraint is violated |
|
|