0

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?

3
  • 1
    Use different tables. Obviously students and teachers are different Commented 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. Commented 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. Commented Jun 3, 2021 at 15:50

1 Answer 1

2

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" ...
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.