QField icon indicating copy to clipboard operation
QField copied to clipboard

Differences between QGIS and QFIELD when adding entities - ERROR ?

Open Ipickedausername opened this issue 3 years ago • 2 comments

Hello,

In QGIS, I want to add +1 to the ID when the entity is newly created, only if another field = 02. This modification shouldn't occur when editing the entity (for modifications).

I'm using this expression as default value :

CASE WHEN "COMP_Val" = 02 THEN CASE WHEN count("id_sondage") = 0 THEN 1 WHEN count("id_sondage") >= 1 THEN maximum ("id_sondage") + 1 END ELSE 0 END

When "field" = 02 [the expression can be execute otherwise : = 0] WHEN count("id_sondage") = 0 THEN 1 [Obliged to add this part, so the first entity = 1, otherwise it's not working] WHEN count("id_sondage") >= 1 THEN maximum ("id_sondage") + 1 [For each new entity, add +1 to ID]

I also tried this expression : CASE WHEN "COMP_Val" = 02 THEN CASE WHEN "id_sondage" is NULL then 1 ELSE maximum("id_sondage") + 1 END ELSE 0 END

On QGIS, this is working well. When adding entities, the ID is well updated.
On QFIELD, the ID is always = 0

Expected behavior:

I'm expected the same behavior on QFIELD than on QGIS.

Observed behavior:

Difference between QGIS and QFIELD

Screenshots and GIFs

1_ok 2_ok 2_not_ok

Please find attached a project QGIS and QFIELD I you want to take a look.

QField version: 1.10.0 (1da006 v1.10.0), master-dev (06ebf1)

Additional information:

  • Problem started happening recently, didn't happen in an older version of QField: [No]
  • Problem can be reliably reproduced, doesn't happen randomly: [Yes]
  • Problem happens with all files and projects, not only some files or projects: [Yes]

Test_field.zip Test_QGIS.zip

Thanks for your attention,

Bye !

Ipickedausername avatar Mar 25 '22 07:03 Ipickedausername

What version of QGIS are you using? I tested out your test_field and test_QGIS projects on QGIS 3.16.13 and 3.22.0 and still got default values of 0 for id_sondage.

Firstly, it helps to use a different result when testing/catching errors so that you can narrow down the cause. So for example in your expression you could try CASE WHEN "COMP_Val" = 02 THEN CASE WHEN "id_sondage" is NULL then 1 ELSE maximum("id_sondage") + 1 END ELSE 999 END so that if you are having problems with COMP_Val it shows up as 999, and if the issue is somewhere else (e.g. autoincrementation, or id_sondage) you get 0.

Secondly - as it turns out I think there are two issues. The use of virtual field values when autocalculating other values, and your autoincrementing expression.

Issue 1: Virtual fields

I think one reason you aren't getting the desired result because COMP_Val is a virtual field (that you've also applied a default expression to). Access to the stored value of a virtual field may not be as reliable as an actual field. Your tab visibility responds to it as well as the displayed value in the form. But the default expression in id_sondage can't pick it up (not in my versions of QGIS anyway).

Using the above testing expression I kept getting a default id_sondage value of 999.

So I replaced COMP_Val in that expression with the default expression that was used for that field (i.e. replace "COMP_Val" with string_to_array("COMPART", '-')[0]) = 02) and I no longer got 999. I also tried it out with reference to another field I created, COMP_Val2 which was not a virtual field but a real one, just with the same default expression as COMP_Val (so would show the same thing and function in almost the same way). And I also no longer got 999.

Solution to issue 1: either use an actual field with a default expression (instead of a virtual field), or just use the default expression itself in other calculations.

Issue 2: Autoincrementing Despite the above fix I still then kept getting a value of 0 for id_sondage. But if I turned off the expression constraint and digitised a feature with a value of 0, subsequently digitised features would autoincrement to 1, 2,... afterwards.

So then there was an issue with how you get the first feature to start at 1. Your first default expression using count() worked so you would start from 1 (I assume this is what you used in your screenshotted examples - the issue there is then back to #1 how virtual fields are handled).

With the zips you uploaded though, it used your second default expression which didn't work (on my versions of QGIS), because you need to refer to the current, unsaved value that appears when you open the form (which is NULL) not just id_sondage which currently starts at 0 due to how yoru Range widget is set up.

Solution to issue 2: one of three options -

(a) Instead of "id_sondage" IS NULL you need current_value("id_sondage") IS NULL.

(b) Alternatively try making the minimum value in the Range widget 1.

(c) Alternatively instead of a CASE statement use coalesce(maximum("id_sondage"),0) + 1

Sorry it's a long reply but there's a lot to go through since you have a very complicated set up with so many rules. I would suggest with these kinds of questions try asking them on GIS.Stackexchange as well since it'll get more eyeballs and often the issue isn't necessarily to do with QField but the understanding and application of fields/forms/expressions.

she-weeds avatar Mar 31 '22 02:03 she-weeds

Thank you very much for all these explanations !

I tried so many solutions during weeks, I finaly thought the problem was coming from QFIELD.

I tried your solutions and your advices.

At the end, it is working on QGIS, but still not in QFIELD.

Here what I'm trying to do : Add +1 to an ID when "COMP_Val" = "'02" Else ID = NULL

When I need to add +1 to the ID If ID is NULL then ID = 1 (because maximum("ID") on NULL value = NULL) If ID > 0 then Maximum("ID") + 1

According to me the issue is coming from the Default value or Expression for Udapte Value. There is a default value on my field "ID" but this default value isn't updated on changes (checkbox unchecked)

On QGIS, when I add an entity, the value of "COMP_Val" = NULL ; then I choose '02-Soil survey' and the value of "COMP_Val" become '02' (because COMP_Val is automatically updated)

      since `"COMP_Val"` = 02 then `"ID"` = 1 or `"ID"` + 1

Here the update value is unchecked, that means the field is updated until save. If I edit the others fields, no more change for this field.

On QFIELD, when I add an entity, the value of "COMP_Val" = NULL ; then whatever I do, since the value of "COMP_Val" was NULL, ID = NULL (or 999)

If default value is updated on changes, then it is working. QFIELD will update "ID". "COMP_Val" still become '02' and "ID" can be updated to "ID" + 1

Don't you think there is a problem on the way QFIELD update fields ?

I add 2 new projects on QFIELD ; one with auto updated "ID" and the other with no update. The two are working the same on QGIS, but not on QFIELD. And I can't opt for the updated "ID", because it will drive to others problems for me.

I'm using QGIS 3.22.4 and QField version: 1.10.0 (1da006 v1.10.0) and master-dev (06ebf1)

Thank you again for your answer.

Test_QFIELD_not_auto_update.zip Test_QFIELD_auto_update.zip

Ipickedausername avatar Mar 31 '22 04:03 Ipickedausername