My application has 2 types of users, so I'm employing one of the standard approaches to design the schema:
Table users (holding fields shared between both user types):
+----+----------------+---------+
| id | email | type_id |
+----+----------------+---------+
| 1 | foo@bla.com | 1 |
| 2 | bar@bla.com | 2 |
| 3 | bla@foobar.com | 1 |
+----+----------------+---------+
Table user_types:
+----+----------+
| id | name |
+----+----------+
| 1 | person |
| 2 | company |
+----+----------+
Table persons (first user type):
+----+---------+------------+-----------+
| id | user_id | first_name | last_name |
+----+---------+------------+-----------+
| 21 | 1 | John | Smith |
| 23 | 3 | Sohn | Kmith |
+----+---------+------------+-----------+
Table companies (second user type):
+----+---------+--------------+
| id | user_id | name |
+----+---------+--------------+
| 31 | 2 | Company Inc. |
+----+---------+--------------+
The user_id
field in persons
and companies
is a foreign key referencing users.id
.
Now, at some point I need to create user profile pages, and the URLs will use the ID of the user, like this:
- /company/[ID]-company-inc
- /person/[ID]-john-smith
My question is:
Which ID should I use to identify the user? Which ID should be the "user ID" that will be used across the app?
Should it be the one from the users
table (users.id
), or should it be the one from one of the respective tables (persons.id
or companies.id
), as they both have their own primary keys?
My assumption is that every table should have a primary key, so I would go with the ID from any of the user-specific tables - persons or companies.
Is this approach correct?