What's the easiest method to model relationships between customers for any social media site?

The potential states are:

  • no Friendship
  • FriendRequest from One place to another, B must confirm (this really is asymmetric)
  • A and B are buddies (this really is symmetric)

now its complicated to find the models right.

My Buddies are members of my profile

quite apparent, A.profile.buddies is really a many to a lot of regards to other customers.

  • no friendship: B not inside a.buddies along with a not in B.buddies
  • A demands friendship with B: B inside a.buddies
  • Buddies: B inside a.buddies along with a in B.buddies

however it appears to become rather unclean to merge the friend using the friendrequest relation. and without it merge, the information is redundant, because then "A in B.buddies and never B inside a.buddies" could be an undefined condition.

Friend-Research: A.buddies.filter(buddies__consists of=B) #rather complicated research on db level, unintuitive for programmers

Seperate tables

FriendRequest is very apparent, a category with requester and asked for_user, the chooses are very apparent, too.

The Friend Model could be not so nice, since it might have person1 and person2 as fields, and all sorts of searches have to choose Buddies with person1=A and person2=B OR person1=B and person2=A

Friend-Research: Friend.objects.filter(person1=A) union Friend.objects.filter(person2=A) #unclean with the necessity to union two sets

Seperate many2many table

another option is a buddy model having a buddies area, the industry many2many area, which links to exactly two persons. Then your choose is matching among the persons within the buddies area, after which just returns the model, in which the person B could be extrated by substracting A in the buddies set. But this is overkill, because no friend-object would have a lot more than 2 persons connected.

Friend-Research: Friendship.objects.filter(persons__consists of=A) #queries two tables

So, what is your opinion may be the cleanest and many intuitive means to fix storing a friendship-relation? What are the common designs how to get it done?

To have an sql db, I'd use many to a lot of relationship with this. But when you believe that you may have lots of customers You might like to consider graph databases like flock-db that is particularly created for this type of data

http://en.wikipedia.org/wiki/Graph_database

(Keep the regular data on sql db and associations on graph database)

In my opinion this can be a use situation for that extended many-to-many relationship based on Django: https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

Rather than just storing the bond between individuals customers, you are able to store additional qualities. This will project your condition domain to the DB model pretty much. Ie make your connection the moment among the two initiates the friendship, then set additional fields to keep who's asking whom and whether your partner has recognized the friendship.