How to reference current instance in stored calc expression

I’ve following types Child and Parent

entity type Parent {
  children: [Child](parent)

entity type Child {
  parent: Parent
  field: string

Now I want to add a stored calc field on Child that filters on same values for field and picking latest one (by creation date). I tried this expression:

  latestField: Child stored calc 'parent.children.(field==field)[0]'

In the expression above, I need first field to be from the parent’s children list and second field to reference current instance. Is this possible?

This is how I’ve seen it done:

entity type TypeName schema name 'SCHEMA_NAME' {

  * Reference to an element's self to fulfill {@link PiElement.plant} stored calc
  self: [TypeName](id, id)


then make your stored calc off of the self field.

1 Like

What you’re trying to do might be similar to this post: Foreign key field matching multiple conditions?

One of the approaches I tried unsuccessfully was this:

entity type Foo {
  oneBarObjs: [Bar](barOne, fooOne)
  barObjs: [Bar] stored calc 'oneBarObjs.(barTwo == this.fooTwo)'
  // Calc field barObjs in type -type-Foo.Foo references field fooTwo which
  // is not defined in type -type-Bar.Bar or any of its extension types

The solution there was to create a composite key field:

1 Like
1 Like

This might be relevant @bachr

Thanks everyone, this seems to do the job:

entity type Child {
  fieldKey: string stored calc " + '#' + field"

  child: [Child](fieldKey, fieldKey)