I would like to design a message inbox table schema for messages which may belong to individual users or user groups.
Let's say we have these tables already:
- users table with uid (PK)
- groups table with gid (PK)
- groups_association table with gid (FK) and uid (FK)
Then, what would be the best design of the message and message-post tables to satisfy these requirements:
- any individual user can message another user
- any individual user can message multiple users
- any individual user can message ALL users in an individual group (the number of users within a particular group can vary over time)
- you should be able to query for all messages that belong to a specific user
My current thinking goes as this:
- Set up three tables:
- message with mid (PK) and let's say the subject
- message_participants with mpmid (FK), mptype ENUM('user', 'group') and mpid (FK of the type)
- message_posts with msgmid (FK) referencing the message table PK
- To get all messages
- get all groups for a particular user
- query message_participants WHERE (mptype = 'user' AND mpid = uid) OR (mptype = 'group' AND mpid IN (all user groups))
This way you could even send messages to individual users and groups at the same time.
Is this how you would normally approach this type of problem?