5

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:

enter image description here

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:

enter image description here

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.

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Mar 9, 2022 at 7:12
11
  • 2
    the result of your expression is an array and therefore if the field is not correctly defined you will always get NULL Commented Mar 9, 2022 at 7:55
  • Thanks for the clarification. Apparently this is not the only problem, however. Commented Mar 9, 2022 at 8:17
  • Can you share your sample data set and the project? This would make it much easier to test - otherwise, one has to rebuild the whole project/data... Commented Mar 9, 2022 at 9:04
  • Ok, edited the original again. Commented Mar 9, 2022 at 9:23
  • You are sure the field you are calculating is a Text field with sufficient length to hold the string? Commented Mar 9, 2022 at 9:30

1 Answer 1

4

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 () - or array_last () for the last one.
  • To get any element of the array, use array_get ( )[x] and replace x with the index of the element you want to get (0 for the first one).
answered Mar 9, 2022 at 8:09
3
  • 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. Commented 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? Commented Mar 9, 2022 at 8:20
  • I edited the original question. I hope that clarifies the question. Commented Mar 9, 2022 at 8:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.