Skip to content

Commit

Permalink
Also check for nulls when applying defaults
Browse files Browse the repository at this point in the history
isValid is not enough because fields are initialized with
QVariant(field.type()) which is valid but null.

Fixes #21304

With test
  • Loading branch information
elpaso committed Feb 20, 2019
1 parent 76c6ec8 commit bf1575f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/core/qgsvectorlayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,

// 2. client side default expression
// note - deliberately not using else if!
if ( ( !v.isValid() || ( hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
if ( ( ! v.isValid() || v.isNull() || ( hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
&& layer->defaultValueDefinition( idx ).isValid() )
{
// client side default expression set - takes precedence over all. Why? Well, this is the only default
Expand All @@ -506,8 +506,8 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,

// 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() || ( hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
if ( ( ! v.isValid() || v.isNull() || ( hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
&& fields.fieldOrigin( idx ) == QgsFields::OriginProvider )
{
int providerIndex = fields.fieldOriginIndex( idx );
Expand All @@ -521,8 +521,8 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,

// 4. provider side default literal
// note - deliberately not using else if!
if ( ( !v.isValid() || ( checkUnique && hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
if ( ( ! v.isValid() || v.isNull() || ( checkUnique && hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
&& fields.fieldOrigin( idx ) == QgsFields::OriginProvider )
{
int providerIndex = fields.fieldOriginIndex( idx );
Expand All @@ -536,7 +536,7 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,

// 5. passed attribute value
// note - deliberately not using else if!
if ( !v.isValid() && fd.attributes().contains( idx ) )
if ( ( !v.isValid() || v.isNull() ) && fd.attributes().contains( idx ) )
{
v = fd.attributes().value( idx );
}
Expand Down

0 comments on commit bf1575f

Please sign in to comment.