I have a layer with a relate table (1-M) with three columns (fk_id, date, comment).
fid | fk_id | date | comment |
---|---|---|---|
1 | 01 | 2021年02月03日 | Comment 1 |
2 | 02 | 2021年02月05日 | Comment 2 |
3 | 02 | 2021年03月02日 | Comment 3 |
4 | 02 | 2021年03月25日 | Comment 4 |
5 | 03 | 2021年01月11日 | Comment 5 |
I have also created a custom form for the layer and table.
What I need is when I add a new record to the related table, the default comment must be the same as the last date for that fk_id
.
So, in this example, if I create a new record in the related table using the form, the comment should be Comment 4
.
I'm looking around the expression builder but I have no idea how to do this selection.
Anyone could give me a tip of how can I set this default?
-
Just to check I got this right: Based on your ID, you want to grab the comment of the newest of the already existing features with the same ID?Erik– Erik2021年06月24日 08:41:13 +00:00Commented Jun 24, 2021 at 8:41
-
That's right (I guess I didn't explain myself well in the question).Raúl Casado– Raúl Casado2021年06月24日 08:54:10 +00:00Commented Jun 24, 2021 at 8:54
-
It's always tricky to explain familiar issue to others ;-) anyway, to my knowledge it is not possible to automatically base one attribute of a new feature on something you're entering while creating that feature.Erik– Erik2021年06月24日 08:59:57 +00:00Commented Jun 24, 2021 at 8:59
1 Answer 1
To add a new record to the related table, with the default comment the same as the last date for that attribute, please set up some defaults through RMC > Properties > Attribute Form > Fields > date > Defaults
I set map_get(attributes(get_feature_by_id(@layer, maximum("fid"))), 'date')
as the 'Default value' as well as ticked the 'Apply default value on update' and clicked Apply.
So, now after adding a new feature it will possess the date of the last feature.
Mind that maximum()
function also includes group_by
argument.
For the example of the question, a working expression could be:
map_get(attributes(get_feature_by_id(@layer, maximum("fid", group_by:="fk_id"))), 'comment')
References:
-
Thanks Taras, I used your expression using group_by for maximum():
map_get(attributes(get_feature_by_id(@layer, maximum(fid, fk_id))), 'comment')
Raúl Casado– Raúl Casado2021年06月24日 10:03:59 +00:00Commented Jun 24, 2021 at 10:03