4

CONTEXT

My data consists of a Point-shp (parent layer), a table (child layer), and a 1-N relation between these layers. The point layer represents trees. In this point-layer there is an attribute called Plot_ID, indicating where the tree is located.

After creating a tree, several characteristics of the tree can be inserted (CREATING FEATURES IN CHILD LAYER), each characteristic created would be a feature in the table layer.

QUESTION

In the child layer there is also a specific column for indicating the Plot_ID. Now, I'd like to copy the attribute value of the specific feature of the parent layer to the feature that is being created from the child layer. This is, if parent is called e.g. 'A', the features from the child from this parent 'A', will copy this Plot_ID.

For aclaration, the process would consist as follows:

1) Create point

1.1) Fill atributes:

fid = 1

uniqueID = 2abc-3cde-4fgh

Plot_ID = P1

2) Create feature in child layer

2.1) Fill attributes:

fid = 1

fk_field = 2abc-3cde-4fgh

Plot_ID = P1 --> through an automatic expression

What I'm looking for is to automatically set an expression that gets the attribute value of the parent layer (Plot_ID), when creating a feature in the child layer. Then, everytime I create a point, and then a child feature, it will automatically get the attribute value Plot_ID from its parent feature.

What I've tried

I've already tried approaches like 1., relation_aggregate, and expressions like attribute(@parent, 'Plot_ID'), but none of them gave me the result I was looking for. In the case of relation_aggregate, the relation between my two layers was somehow not recognized and gave me no value as an output (found no solution about this in internet either).

  1. Aggregating child features with parent attribute values in QGIS
3
  • Does this help? gis.stackexchange.com/q/442304/128665. In place of 'myfieldvalue' you can substitute the field name (double-quoted - "fieldname"), which will take the attribute value of the current feature (in this case 'A').
    – Matt
    Commented Dec 16, 2022 at 19:12
  • 1
    @Matt this wouldn't work in my case. I'm looking for an automatised approach, so that everytime a child layer feature is created, it copies automatically the attribute value from its parent layer. The approach from the link you sent would work for individual cases where no automatisation is required.
    – Astro
    Commented Dec 17, 2022 at 8:52
  • Can you clarify.... Are you hoping that on creation (save) of the point feature, rows (features) are automatically added to the related table? Option 1 Or will you manually add child records Option 2 For option 1, you cannot use Python is you want it to work in QField. If you use PostGIS or SQLite or GPKG then you can create ON INSERT rules for the table, but this doesn't work for SHP. For option 2, create the relation, and add the relation to your atrtibute form. It will automatically populate the fk field with the parent's primary key - assuming your relation is based on these.
    – Oisin
    Commented Dec 17, 2022 at 18:18

1 Answer 1

4

You shouldn't aggregate values, because you said the relationship is one-to-many. So there will only be one tree with the same uniqueID as the current characteristic fk_field, and from that tree you want to extract the Plot_ID attribute:

attribute( get_feature( 'trees', 'uniqueID', current_value( 'fk_field')), 'Plot_ID')

Both relation_aggregate function and @parent variable will not work here because they are intended to be used in parent table, where you can aggregate child features.

The expression was tested in memory layers and it work. Might be problems in not-database providers when trying to create a new child feature from the parent form, when the parent feature is not already saved.

5
  • I've tried to use it but I'm not sure how the field ´name´ should be replaced in my case. I tried: attribute( get_feature( 'trees', 'uniqueIDfromPARENT', current_value( 'uniqueIDfromCHILD')), 'Plot_IDattributefromPARENT'). As far as I understood, this code asks for the attribute Plot_ID of a feature from the parent, where uniqueIDfromPARENT = uniqueIDfromCHILD. Am I misleading the concept?
    – Astro
    Commented Dec 17, 2022 at 9:32
  • You said a tree was called A, so I assumed it had a 'name' field with attribute A. The same for the characteristics table, but not a unique id in that case, because you had a one-to-many relation, can be more than one characteristic with the same tree name. I edited the answer to clarify the foreign key field. Commented Dec 17, 2022 at 11:53
  • Sorry for the misunderstanding. What I meant with same name, is that they share the same unique ID. In case of the parent, this is called 'uniqueID'. In the case of the child, this is called 'fk_field'. I edited the original text of the question to clarify how looks the result I'm looking for.
    – Astro
    Commented Dec 17, 2022 at 17:11
  • I edited the answer again to reflect the same field names. I was trying to test the expression against a shapefile parent layer and can't make it work for the case of create a new parent feature and create child features without saving the changes in the parent layer. Commented Dec 17, 2022 at 21:37
  • 1
    This expression works. However, as you said, the layer has to be saved beforehand. I mark it as a solution. Thanks!
    – Astro
    Commented Dec 21, 2022 at 14:11

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