I have a data table that holds different information about buildings in a GeoPackage. Different elements of the buildings can be entered, e.g. sometimes I only have the building as a whole, sometimes I have an entry for each room. Each entry has a "UUID"
as a unique key. I use QGIS 3.16.3.
A simplified version of the table looks like this: enter image description here
You can download the simplified sample data here.
I added a field "uuid_parent"
and created a recursive relation, which works fine, e.g. I can link a room to the building the room is in via the buildings "UUID"
. Now I would like to fill in all the information that necessarily are identical with the parent automatically, e.g. the address or the postal code.
The relation is defined like this:
I tried using a relation_aggregate()
function as the field default in the layer properties on the field address like this:
relation_aggregate(
relation:='entities_d_uuid_parent_entities_d_uuid',
aggregate:='array_agg',
expression:="address"
)
Unfortunately I only get NULL
-Values so far. I created a small sample data set, that shows, how it is supposed to look in the end. In this case, I had to type in the entries in the field "address"
by hand, however:
Edit after hints by @pigreco and @Babel:
I edited the default expression into the following:
array_to_string(
relation_aggregate(
relation:='test_test',
aggregate:='array_agg',
expression:="address"
)
)
This shows me the correct values, but only in the preview of the expression and not in the attribute form or table when entering data.
1 Answer 1
As @pigreco mentioned, the result of your expression is an array. To convert it to either a text or a number, you have different options (see documentation):
- To convert the array to a text (string), enclose your expression in an
array_to_string( )
function. - To get the the first element of the array, use
array_first ()
- orarray_last ()
for the last one. - To get any element of the array, use
array_get ( )[x]
and replacex
with the index of the element you want to get (0 for the first one).
-
Thanks for the explanation. I tried all three variants, but I the result did not change. Two things appear as problems to me: 1) when entering data into the field "address" it jumps back to NULL, so I have to enter the data at least twice. 2) No matter what I enter as a parent feature, the field "address" remains NULL.C.-F. Vintar– C.-F. Vintar2022年03月09日 08:17:12 +00:00Commented Mar 9, 2022 at 8:17
-
You must provide more information. Can you provide sample data and show us how your attribute table looks like? Have you defined field-type and length correctly?Babel– Babel2022年03月09日 08:20:13 +00:00Commented Mar 9, 2022 at 8:20
-
I edited the original question. I hope that clarifies the question.C.-F. Vintar– C.-F. Vintar2022年03月09日 08:55:19 +00:00Commented Mar 9, 2022 at 8:55
Explore related questions
See similar questions with these tags.
NULL