2

I'm using react-admin with hasura as an API for Postgres.

I have a need for different entities that are all basically a person but with different purposes. Eg. a student and a teacher. I am thinking, given the similar attributes, it would be good to have a person table that will share common features and then have a separate tables for specific attributes. For example, tables like this:

persons {
    id,
    first_name,
    last_name,
    birthday
    ...
}

students {
    id,
    person_id,
    date_enrolled
    ...
}

teachers {
    id,
    person_id,
    sallary
    ...
}

The problem I encounter with that schema is that in order to create a student, the person for it has to be created first. So in react-admin I will have to present a user with a form for creating a person first and then another form to create a student based on that person.

I guess it's not ideal and maybe it would be better to have separate tables for students and teachers although some fields (first_name, last_name, birthday...) would be repeated in both tables.

This question is not really specific for react-admin but more of a general advice on similiar db model design so any tips are more than welcome :).

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Andrija Ćeranić
  • 1,633
  • 11
  • 14
  • I am not familiar with the tools you are using, but can you create a single form with all the data needed for both tables? Internally, the software would first insert the person data and, with that, insert the student / teacher data... – Antonio Veneroso Contreras Feb 28 '19 at 16:25
  • I could do that, sure, but it doesn't seem easily doable with react-admin. – Andrija Ćeranić Feb 28 '19 at 16:41

1 Answers1

1

This requirement is a perfect fit for PostgreSQL's “table inheritance” feature.

You could model student and teacher as inheritance children of person. Then everything should work as you want. Querying person will return results from both tables.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263