How to cascade delete in one to many entity relation ?

I have two entities Category and Product having one to many relation. I am new to outsystems and find an example on documentation to cascade delete entities with one to one relationship. But in the 1-m scenario, I am unable to figure out how to use foreach for multiple products for a category. 



I have created an Rest Api to delete category having flow as follows:


Solution

Hi Shikha,

If you set the delete rule of the CategoryId attribute to Delete, you will guarantee that when a category is deleted from the database all the products with that category are also deleted.

Regards!

Solution

Hi Shikha,

To add what Bruno corretly states, here is the official OutSystems documentation regarding the delete rule:

https://success.outsystems.com/Documentation/11/Developing_an_Application/Use_Data/Data_Modeling/Entity_Relationships/Delete_Rules

Regards,

Daniel

ok. But do I need to do anything in the workflow ?


Yes.

If you cascade the Delete, remove the IF and the DeleteProduct, as deleting a category will delete all products associated with it.

But remember, if those products are referenced on other entity, this may not work, if the other entity FK has a Protect rule.

As for iterating, if required, you would need to do an Aggregate to fetch the products, filtered by the category, and use a ForEach statement over the output list of the Aggregate. Then is a matter of using the Current of the list on each iteration.

Cheers.

Hi Shikha,

If you're not using the delete-rule, you need to use the for-each widget instead. In your examply you are ony deleting 1 product.

Regards,

Lennart