Authentication related question (User entity and Employee entity relationship)

I am developing an app with the following requirement:

  • The app have authentication system ( generated using phx.gen.auth)
  • The app also have a context Staff that uses employees table to store employee related data.
  • An user account may be created with or without a related employee entity.
  • An employee record may be created with or without an user account for authentication.

What is the best way to design the above requirement?

You can represent a one-to-one relationship like this with belongs_to and has_one - either schema can be the one with the foreign key column (user_id on employees, or employee_id on users).

If the relationship stays one-to-one, the two approaches are identical. If, in the future, the relationship needs to expand, the difficulty of that will depend on the choice made:

  • if user_id is added to employees, it is easy to expand User has_one :employee to User has_many :employees.
  • if employee_id is added to users, it is easy to expand Employee has_one :user to Employee has_many :users

The second scenario seems more useful - for instance, imagine users wanted to be able to log in with either their “work email” or their “personal email” - so I’d choose it unless there’s a not-mentioned reason not to.


@al2o3cr thank you so much