On Sun, Mar 06, 2005 at 05:42:14 +0100, Stefan Weiss <spaceman@foo.at> wrote:
>
> We are currently designing a web-based application in which users can
> add other users as "friends". These links are bi-directional, meaning
> that when A adds B to his friends, he is automatically one of B's
> friends. Eventually we will have to add a feature that shows how A is
This doesn't seem like a good idea unless the person getting linked to
gets to confirm he wants the link creator as a friend.
> I can see several ways how such links could be modeled in a relational
> database, but I was wondering if there was some tried-and-true recipe
> that would spare me from reinventing the wheel. Putting aside for the
> moment everything but the links, the simplest way of connecting users
> would be a "friends" table (user_id int, friend_id int). We could get a
> user's friends with a simple query like this:
>
> SELECT friend_id FROM friends WHERE user_id = X
> UNION SELECT user_id FROM friends WHERE friend_id = X;
It would probably be better to always have either both or neither of
the symmetric relationships in the table. You could make a set of triggers
to enforce this.