Skip to content

Commit

Permalink
Recalculate "evaluate on update" default expressions always
Browse files Browse the repository at this point in the history
... no mather what attributes are provided. Default values with evaluate on update
should really just always be calculated.

Fix #30164
Fix #29683
  • Loading branch information
m-kuhn committed Jun 12, 2019
1 parent 3113a96 commit 492ce0c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/qgsvectorlayerutils.cpp
Expand Up @@ -507,9 +507,11 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,

// 2. client side default expression
// note - deliberately not using else if!
QgsDefaultValue defaultValueDefinition = layer->defaultValueDefinition( idx );
if ( ( v.isNull() || ( hasUniqueConstraint
&& uniqueValueCaches[ idx ].contains( v ) ) )
&& layer->defaultValueDefinition( idx ).isValid() )
&& uniqueValueCaches[ idx ].contains( v ) )
|| defaultValueDefinition.applyOnUpdate() )
&& defaultValueDefinition.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
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsvectorlayerutils.py
Expand Up @@ -263,6 +263,10 @@ def testCreateFeature(self):
# we do not expect the default value expression to take precedence over the attribute map
f = QgsVectorLayerUtils.createFeature(layer, attributes={0: 'a', 2: 6.0})
self.assertEqual(f.attributes(), ['a', NULL, 6.0])
# default value takes precedence if it's apply on update
layer.setDefaultValueDefinition(2, QgsDefaultValue('3*4', True))
f = QgsVectorLayerUtils.createFeature(layer, attributes={0: 'a', 2: 6.0})
self.assertEqual(f.attributes(), ['a', NULL, 12.0])
# layer with default value expression based on geometry
layer.setDefaultValueDefinition(2, QgsDefaultValue('3*$x'))
f = QgsVectorLayerUtils.createFeature(layer, g)
Expand Down

0 comments on commit 492ce0c

Please sign in to comment.