How to ensure referential integrity in the database?

2
Just playing with my trial version, I experienced a challenge for which I cannot find a solution myself. Suppose I have a domain model with the entities Customer and Order and an association Order_Customer which associates each order with one customer. Now I noticed that it is possible to insert an order without an association with a customer. At first I tried to solve it by adding a CustomerName property to the Order through a microflow, but I cannot place a Required validation on a microflow attribute. My second attempt was to add a Before Commit event on the Order entity that returns true if a customer association exists and false if not. I tested the outcome of the microflow with a Show Message action and it works. It returns true or false just as expected. Now the only problem is that even if it returns false, the order is still committed to the database table. My question: What is the right procedure to ensure referential integrity between entities?
asked
4 answers
3

The before commit should not allow your Order object to be commited if you returned false. Are you sure that your return value is false and not just the equation you make? Look at your end point what does it return? Also debugging can help you here. Set a breakpoint on your before commit to see what it actually returns. As said earlier, if it returns false it should not commit the object.

answered
1

Check it before you save the object by using a microflow instead of a default save button.

In that microflow you can use a show validation feedback action if something is wrong, or a change & commit activity if everything is OK.

answered
1

Found it!

In the event microflow I retrieved the Customer from the database instead of from association. The effect is an implicit commit of the Order during the event, thereby bypassing the event handler. Returning false after the commit has no effect because the Order has already been saved.

Now I'm just wondering if this is what we want. Event handlers are meant to safeguard data integrity. Should it be possible to inadvertently bypass them and ruin your data?

answered
0

Unfortunately, breakpoints don't seem to work in the trial version, but the Show Message action just before exiting the microflow confirms that it returns either true or false just as I expect it to do.

I know the Before Commit event gets fired at the right time and that it returns the correct boolean value. Could it be possible that the trial version doesn't respond to return values of event handlers?

answered