Skip to content
Permalink
Browse files

take original for unique values as well, except if it's not unique

  • Loading branch information
signedav committed Oct 15, 2018
1 parent 67276dc commit 963fdd5a7e2f29c0e282869681f107ab9f78c9aa
Showing with 7 additions and 7 deletions.
  1. +7 −7 src/core/qgsvectorlayerutils.cpp
@@ -381,16 +381,15 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons

// in order of priority:
// 1. passed attribute value and if field does not have a unique constraint like primary key
if ( attributes.contains( idx )
&& !( fields.at( idx ).constraints().constraints() & QgsFieldConstraints::ConstraintUnique ) )
if ( attributes.contains( idx ) )
{
v = attributes.value( idx );
checkUnique = false;
}

// 2. client side default expression
// note - deliberately not using else if!
if ( !v.isValid() && layer->defaultValueDefinition( idx ).isValid() )
if ( ( !v.isValid() || ( fields.at( idx ).constraints().constraints() & QgsFieldConstraints::ConstraintUnique && QgsVectorLayerUtils::valueExists( layer, idx, v ) ) )
&& 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
@@ -400,7 +399,8 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons

// 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 )
if ( ( !v.isValid() || ( fields.at( idx ).constraints().constraints() & QgsFieldConstraints::ConstraintUnique && QgsVectorLayerUtils::valueExists( layer, idx, v ) ) )
&& fields.fieldOrigin( idx ) == QgsFields::OriginProvider )
{
int providerIndex = fields.fieldOriginIndex( idx );
QString providerDefault = layer->dataProvider()->defaultValueClause( providerIndex );
@@ -413,7 +413,8 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons

// 4. provider side default literal
// note - deliberately not using else if!
if ( !v.isValid() && fields.fieldOrigin( idx ) == QgsFields::OriginProvider )
if ( ( !v.isValid() || ( fields.at( idx ).constraints().constraints() & QgsFieldConstraints::ConstraintUnique && QgsVectorLayerUtils::valueExists( layer, idx, v ) ) )
&& fields.fieldOrigin( idx ) == QgsFields::OriginProvider )
{
int providerIndex = fields.fieldOriginIndex( idx );
v = layer->dataProvider()->defaultValue( providerIndex );
@@ -431,7 +432,6 @@ QgsFeature QgsVectorLayerUtils::createFeature( const QgsVectorLayer *layer, cons
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

2 comments on commit 963fdd5

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Nov 19, 2018

@signedav

This comment has been minimized.

Copy link
Contributor Author

@signedav signedav replied Nov 19, 2018

@gioman I will.

Please sign in to comment.
You can’t perform that action at this time.