Bit of a 'best practice' question as I am new to DB design and I wanted to make sure I am on the right tracks with this one
I have 3 user types, user (single person), group (lots of users) and company (lots of groups), each has their own login which allows them to post messages. So eg. if a company posts a message it will appear in all the linked users news feeds.
To achieve this I have a table 'messages' that stores the message contents, along with the foreign keys to link the user types
I was going to use the following schema (PostgreSQL) to achieve this...
create table notifications(
notification_id serial primary key,
user_id integer references users,
group_id integer references groups,
company_id integer references companies,
date_created timestamp not null default now(),
title_id text not null,
message_id text not null,
icon text not null default 'logo'
);
comment on table notifications is 'Messages to be displayed on a users home feed';
This would allow me to construct a query that pulls out the relevant messages for a users news feed (eg. only one field user_id, group_id or company_id will have a value)
But is this the best method? I am sure that having nullable foreign keys is a bad idea, I was thinking there might be a better solution using a kind of enumerate key? (Does this even exist?!)
Thanks