tags:

views:

24

answers:

2

I currently have designed a system for simple send message receive message using such simple table. Now I need an extra information that is which message belongs to which conversations. Any ideas tips or guidelines on implementing this kind of system?

CREATE TABLE messages (
    ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    FromID INT NOT NULL,   
    ToID INT NOT NULL,  
    Deleted BOOLEAN DEFAULT FALSE,
    SentDeleted BOOLEAN DEFAULT FALSE,
    Subject varchar(255), 
    Message varchar(255),
    DateTime DATETIME                                       
    ) ENGINE=InnoDB;
+2  A: 

just add an int column called parent_message_id and set it to the id of the message you are replying to.

Matt Williamson
I like it. You could use some recursion for the DB query. It would be epic.
mattbasta
Haha, I've done that before.
Matt Williamson
If I use this extra parent_id for each successive message of a specific conversation would the parent be the first initial message or the one right before itself in the conversation?
Either or. Just sort by posted date if you pick the latter.
Matt Williamson
+1  A: 

The more common way, though, is to have a thread table and messages table. When a conversation is started, create a thread record and set the thread_id column of the message record to it. Set all replies' thread_id to that thread as well. That way you can SELECT * FROM messages where thread_id = x

Matt Williamson
I like that actually, let me give that a try, thank you.What kind of extra information should I save in that conversation table?Unique IDWhoInitiatedConversationWhenWasItInitiatedOr I just really need one column ID?
This is probably the most straight forward way. If you put extra info here it will be so you can search easily. For example if you want to list all of the conversations for an inbox, you can just SELECT * FROM threads WHERE user_id = xSo I would store date_created, user_id, subject
Matt Williamson
And of course an autoincrement id
Matt Williamson