255
Views
13
Comments
Recursive Entities Best practice
Discussion

Suppose we have an employee table. An employee reports to a certain manager. Every manager is also an employee. This is an example of recursive database tables. What is the best practice in Outsystems when you have recursive entities? Are both solutions equal?

or

2024-04-02 13-23-43
Gonçalo Ferraria
Champion

Hi Danny,

The best option for your problem is the first option, one entity with a column to the same table.

The option 2 works, but typically is used when an employee reports to more than one person, for example.

Thank you.


2020-09-26 09-10-18
Danny Eeraerts

Thanks Gonçalo for your reply. I take this with me.

2024-04-02 13-23-43
Gonçalo Ferraria
Champion

Hi Danni,

If you don't have other questions, please mark the post as solved.

Thank you

2021-02-18 12-29-06
António Pereira
Champion

Hi Danny,

For your case, the first option seems to be the best. The second option can be used when an employee reports to more than one person (as Gonçalo stated) or when there are specific attributes for the reporting process.


Cheers,

António Pereira

2021-09-06 15-09-53
Dorine Boudry
 
MVP

Hi Danny,

I agree with both that for this simple example, the main difference is that the top solution only allows employees to report to one manager, and that you can't add attributes to the relationship.

In realistic applications, I would think that you would quickly find the first overly simplistic as  

  • the reporting structure can change over time, and you want to capture that information (for example by adding start and end dates in the relation table)
    • employees change departments
    • employees get promotions
    • managers take on other departments
  • employees might have other relationships among each other than just manager/managed (such as they might have an operational manager as well as an hr manager), your employee table can quickly get cluttered with lots of foreign keys to details that are not really related to them personally
  • I could imagine that the department/users allowed to update the home address of an employee are not the same ones and are not using the same screens/actions as the users allowed to change the reporting structure, so having it modelled separately in your data supports a more natural/simple application design

Dorine

UserImage.jpg
Cuong Tran

Hi @Dorine Boudry 

May I ask how to construct a recursive function to get all layer employee report to with the input as Employee ID?

Thank you

2021-09-06 15-09-53
Dorine Boudry
 
MVP

not exactly sure what you are asking, i think you mean server logic, not function ? And what do you mean with 'recursive function' ? 

Is your requirement to get the entire reporting tree from a single manager's id, or the other way around, the entire management tree starting from a managed employee id ?

And what are you going to do with it ?  If you want to display in tree form on the ui, then you could use tree widget, and in that case, the widget itself will construct the tree from the raw data.

Dorine

UserImage.jpg
Cuong Tran

Hi @Dorine Boudry 

Sorry not being clear in my question. The question is about a server action to get entire reporting tree from a single manager's Id. The result of this will be then the input of ztree widget.

I though this server action is going to be a recursive function (which will call itself), but I still don't know how to do it.

2021-09-06 15-09-53
Dorine Boudry
 
MVP


I would let the dbms do the job, using recursive CTE, and as a result a list of employees, each with an id and parent id, in no particular order.   The ZTree widget can deal with that and use the parentid's in there to make the correct tree.


So have a data action with an SQL widget, not an aggregate.

SQL will be something like



Dorine

2021-09-06 15-09-53
Dorine Boudry
 
MVP


I realise I didn't answer you other question : "how do you build a recursive server action ?"

That is a matter of just calling the action inside it's own logic, so is easy to do.  You just have to make sure you don't create an endless running logic, so you need to build in your logic a moment when recursion ends.

Dorine

UserImage.jpg
Cuong Tran

Hi @Dorine Boudry 

Thank you very much for your advice, the script works like a charm as always.

UserImage.jpg
Lim Jing Wei

For the first option, is it true that the "ReportTo" attribute is of type "List of Employee Identifier"? 

UserImage.jpg
Lim Jing Wei

@Danny Eeraerts For the first option, is it true that the "ReportTo" attribute is of type "List of Employee Identifier"?

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.