DEV Community

Discussion on: Is it possible to reference one column as multiple foreign keys? ( PostgreSQL )

Collapse
 
aarone4 profile image
Aaron Reese

I'm not sure what question you are asking. From the perspective of 3NF your relationship appears to be correct: each Salary can be a user and/or a driver, each table holding different information
However...
Business logic would indicate that there should be at least one table 'above' Salary, probably Employee which should also have this user_id as a primary key (or at least a unique constraint)
You can join user and driver directly without going through salary or employee. Inner join to get records that exist in both, left join to get all records in one and records in the other if they exist or FULL OUTER JOIN to get all records in both but on a shared row if they match. If you need data from salaries employee then probably better to start there and Left Join to each if the other tables.

Collapse
 
jacksonkasi profile image
Jackson Kasi

thanks for your idea! 😊

Collapse
 
aarone4 profile image
Aaron Reese

Sorry I didn't read your script in full. The references logic is in the wrong table; it should be in the users_management and driver_management and they should be referencing salary.user_id
As in my comments above, business logic would indicate that salary is not the correct table to be joining to as it has its own different Primary Key (id) along with a creation timestamp, and as salary will change over time you will likely get multiple records for a user in this table. You should have a table holding the employee/user data and join to this instead
Also you have data in the salary table that maybe should be held elsewhere: bank details should be part of the employee details, not salary details (security and modelling) so salary.user_id REFERENCES user(id)