I’ve got a table called users which contains the users on my webpage. The users can make a friendship where user 1 request user 2 for a friendship.
The way it is working now is, that when user 1 sends the request I create a row in the friendship table which contains the two foreign keys: user_id_from and user_id_to and a field called status which can either be 0 (the request is pending), 1 or 2 either the user_id_to has accepted or declined the friendship request, respectively. And lastly the status can be 3 which indicates that one of the users have deleted the friendship.
Is this the right approach, or should I create a temporary table which contains all the pending request, and then (if the request is accepted) it is inserted into a friendship table?
From a logical perspective, what you’re doing is correct, since I would personally consider a pending friendship request a form of friendship which isn’t confirmed.
From a design perspective, I’m not sure how you would create a temporary table each time the user needs to see his list of friendship requests. If the table isn’t temporary, you would have a table named confirmed_friendships and pending_friendships.
I highly suggest that you keep both in the same table, unless there will be lots of columns which are specific to pending_friendships and not available for confirmed_friendships, or vice versa.