5

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.

Custom form

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?

asked Jun 24, 2021 at 8:34
3
  • 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? Commented Jun 24, 2021 at 8:41
  • That's right (I guess I didn't explain myself well in the question). Commented 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. Commented Jun 24, 2021 at 8:59

1 Answer 1

5

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

settings

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:

answered Jun 24, 2021 at 9:11
1
  • Thanks Taras, I used your expression using group_by for maximum(): map_get(attributes(get_feature_by_id(@layer, maximum(fid, fk_id))), 'comment') Commented Jun 24, 2021 at 10:03

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.