3

I have a question about "default value" based on expression.

For exeaple, I have a layer in a GPKG, with 3 attributes: "Number" / "Letter" / "Roman".

For my attribute "Number", I have a value from a list with 1, 2 and 3. enter image description here For the attribute "Letter", this attribute is not editable. In "default value", I have this expression:

IF("Number"=1, 'one', IF("Number"=2, 'two','three'))

enter image description here

This step work fine, and if I change the value "Number", the "Letter" is updated... Perfect!

Now, if I do the same for the attribute "Roman", but based on "Letter", calculated previously:

IF("Letter" like 'one', 'I', IF("Letter" like 'two', 'II','III'))

enter image description here

But it doesn't work. The attribute isn't calculated, and isn't updated when a change occurs. enter image description here

6
  • = is for comparing numerical values. For strings you have to use LIKE.
    – Erik
    Commented Feb 4, 2021 at 10:17
  • It works also with =. But even with like, it doesn't change... it seems that it is not possible to define a default value expression based on a result of an other default value expression!
    – katagena
    Commented Feb 4, 2021 at 10:54
  • If the issue is as simple as in your example, just omit the first default expression when setting the second?
    – Erik
    Commented Feb 4, 2021 at 10:57
  • I don't understand... see my updates above!
    – katagena
    Commented Feb 4, 2021 at 11:08
  • So why not setting the default value expression using the Number field as reference each time
    – J.R
    Commented Feb 4, 2021 at 11:15

1 Answer 1

4

In QGIS' documentation on form default values, it states:

The Default value option is not aware of the values in any other field of the feature being created so it won’t be possible to use an expression combining any of those values i.e using an expression like concat(field1, field2) may not work.

So in your example, you shouldn't expect "Letter" to have been set when the default value for "Roman" is being evaluated. You can't even expect the expression for "Letter" to know about "Number", though it seems in your case, the execution order happens to be such that it does.

In general, in instances where you think you want an attribute which is not editable but set on the basis of other attributes, it's better to use virtual fields. These are recalculated on the fly. This is rather than actual attribute fields with default values, since your user is not expected to change it anyway away from what the logic you have specified prescribes.

1
  • Thank you for the clear answer!
    – katagena
    Commented Feb 4, 2021 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.