I have three user types which I intend to store in three different database tables. The structure is as follows:
- A
mastertable that contains columnsid,teacher_userid,student_userid,parent_userid,roleand a few other columns. - A
teachertable containing details about a teacher user (including login credentials) - A
studenttable containing details about a student user (including login credentials) - A
parenttable containing details about a parent user (including login credentials)
The reason for having three separate tables is that the data stored for each will be quite different and it seems cleaner to do it this way.
I want users of any type to be able to sign in through the same form in my Flask app. flask-login should look up the master table, which should then pull data from the relevant teacher, student or parent table to populate the User model.
However, I'm stumped as to how to populate the User model conditionally with data from the other tables.
The ideal scenario is to be able to add something like this to the User(db.model):
if 'Teacher' in role:
name = teacher.name
email = teacher.email
elif 'Student in role:
name = student.name
email = student.email
And so on. However, it does not appear to be possible to use conditional statements like that within the definition of the User class. Are getters and setters the solution? It all feels quite messy.