I'm making an app that allows teachers and students to log in using the same login form. Teachers will have the same fields as students, except disciplines, and students will have the same fields as teachers, except group. So, how can I do this, to have all the users in the same table, but having different fields?
-
1Use different tables. Obviously students and teachers are differentPanagiotis Kanavos– Panagiotis Kanavos2021-06-03 15:47:49 +00:00Commented Jun 3, 2021 at 15:47
-
This is too broad for SO, but ultimately you could easily have a 'person' table with some fields which are simply not used for certain 'types' of people. If you're properly normalising your data I don't think either 'disciplines' or 'group' would necessarily stay on the 'people' table.JeffUK– JeffUK2021-06-03 15:50:11 +00:00Commented Jun 3, 2021 at 15:50
-
Tables have columns, not fields. A field is either a part of a record data type, or a part of a date/time values.jarlh– jarlh2021-06-03 15:50:24 +00:00Commented Jun 3, 2021 at 15:50
Add a comment
|
1 Answer
What I usually do is separate auth info (users/logins) from domain-specific info (profiles). One way is to link users to profiles polymorphically.
table users
id int not null
profile_id int
profile_type string
password_digest string
last_logged_in_at datetime
# other auth info as needed
table student_profiles
id int not null
group string
table teacher_profiles
id int not null
disciplines string[]
So your student would look like this, for example.
id=1 profile_id=2 profile_type="student_profiles" ...
1 Comment
JeffUK
That's a good approach, the only comment to make is to consider how you handle the situation where teacher is also a student. You don't have to support one login being both, you could have them a teacher login and a separate student login.. just make sure you document the assumption that this is how it works.