I am working on a QField project to take field data of trees, all inside a GeoPackage layer, working in QGIS version 3.40.5-Bratislava.
I have on one side a line data layer arbolado with its geometry and on the other side a non spatial table resumenespecies, both have a 1:m relationship (uui) and are related by a field called id_resumen. The logic of my form in QField is that the user:
- Would fill in the field num_arbol with the number of trees the user sees in the field first within the table arbolado.
- Then he would enter the relational table resumenespecies and distribute the number of trees that he has entered in num_arbol filling a field called esp_ud.
Within this table I have another field restantes that has to take the number of trees entered in the field esp_ud and subtract it from the total number of trees entered in the field num_tree to know how many trees are left to allocate. restantes= num_arbol - esp_ud The goal is to be distributed without going over between different types of forest species that you can also select in a field. In the end several fields will be generated in the relational table with respect to a single geometry (1:m).
I have tried to create the virtual field restantes with the expression:
aggregate(
layer := 'arbolado',
aggregate := 'sum',
expression := "num_arbol",
filter := "id_resumen" = "id_resumen"
) - "esp_ud"
Without success since it seems that it does not take into account just at that moment of creation of the form the data entered in num_arbol. I don't know if you know why it fails. Another expression I tried was:
attribute(@parent, 'num_arbol') - "esp_ud")
And I have also been working unsuccessfully on the expression
attribute(
get_feature('arbolado', 'id_resumen', "id_resumen"),
'num_arbol'
) - "esp_ud"
The data I want to use with all this is from the last geometry record the user creates.
1 Answer 1
Try using the current_parent_value()
function
-
At the moment it seems that the option you have told me is working correctly. Thank you very much. The way I have implemented it has been in the child table, within the field esp_ud, so that when I fill the num_arbol value of the arbolado table (parent table) and then I do the tree allocation as I said above, automatically appears the value of the num_arbol field in the esp_ud field. For new rows within the child table, the value that automatically appears in the field esp_ud is reduced as a result of subtracting num_arbol - esp_ud.Kañi Kañissimo– Kañi Kañissimo2025年05月13日 15:15:38 +00:00Commented May 13 at 15:15
-
coalesce( current_parent_value('num_arbol'), 0 ) - coalesce( aggregate( 'resumenespecies', 'sum', "esp_ud", "id_resumen" = current_parent_value('id_resumen') ), 0 )
Kañi Kañissimo– Kañi Kañissimo2025年05月13日 15:20:07 +00:00Commented May 13 at 15:20