I have a database with some users and agents and want to add a simple message system.
So I have the following simple set of tables:
[users]
- user_id (PK)
[agents]
- agent_id (PK)
[message_threads]
- message_thread_id (PK)
[message]
- message_id (PK)
- message_thread_id (FK messages_threads.message_thread_id)
What I don't have is a relationship from the message and the individual who posted the message.
There is where I'm a bit stuck, because it could be a user or an agent. I figure this must be a common problem with a accepted pattern to solve, but I haven't found such a discussion.
I know I have a few options, but they all have cons.
Option 1: I don't like that a message could be linked two to different accounts.
[message]
- message_id (PK)
- message_thread_id (FK messages_threads.message_thread_id)
- user_id (FK users.user_id, ALLOW NULL)
- agent_id (FK agents.agent_id, ALLOW NULL)
Options 2: This make it awkward to get all messages in one column of a SELECT.
[message_by_user]
- message_id (PK)
- message_thread_id (FK messages_threads.message_thread_id)
- user_id (FK users.user_id)
[message_by_agent]
- message_id (PK)
- message_thread_id (FK messages_threads.message_thread_id)
- agent_id (FK agents.agent_id)
The one thing I can't do in combine users and agents into one table. That is set in stone.