Setting a reference to a new entry in the same transaction

I would like to create an entry and set a reference to it in the same command implementation. I thought it was possible, but I get an error on ‘Afleveradres’ = “Vast afleveradres”

no valid ‘no mandatory reference’ found for ‘type’ option ‘static’. Unexpected ‘existence’:
- expected: ‘optional’
- but found: ‘mandatory’

Command implementation:

'Afleveradressen' = switch @ . 'Vast afleveradres' (
	| 'Nee' => current || empty
	| 'Ja' as $'adres' => ensure (
		'Naam' = "Vast afleveradres"
		'Land' = $'adres' . 'Land'
		'Postcode' = $'adres' . 'Postcode'
		'Bedrijf' = $'adres' . 'Bedrijfsnaam'
	)
)
'Vast afleveradres' = switch @ . 'Vast afleveradres' (
	| 'Nee' => ensure 'Nee' ( )
	| 'Ja' => ensure 'Ja' (
		'Afleveradres' = "Vast afleveradres"
	)
)

Application model:

'Afleveradressen': collection ['Naam'] {
	'Naam': text
	 'Land': text ~> ^ ^ ^ ^ . 'Landen'[]
	'Postcode': text ~> >'Land'.'Postcodes'[]
	'Bedrijf': text ~> >'Postcode'.'Bedrijven'[]
}
'Vast afleveradres': stategroup (
	'Nee' { }
	'Ja' {
		'Afleveradres': text -> ^ . 'Afleveradressen'[] 
	}
)

We plan to ensure that transactions constructed by commands can/will never be rejected by the application/datastore engine. As part of our approach for ensuring that, you cannot use a static text value for a text attribute with a mandatory reference.

On a different note: in your command implementation, you repeat the static text value "Vast afleveradres". When there is a reason to change that value (e.g. for a translation of your app), you would have to update it in two different places/keep the two in sync. Furthermore, you would have to update your application model.

You can prevent that by adding a base value to your app (e.g. in a configuration section): 'Default vast afleveradres': text. You can reference that attribute in your command implementation.

Ok, so if I do that instead of using the static text value inline, it would work?

Yes, that should work.

1 Like