Re: Links between rows in a table - Mailing list pgsql-sql

From Bruno Wolff III
Subject Re: Links between rows in a table
Date
Msg-id 20050306174201.GA19019@wolff.to
Whole thread Raw
In response to Links between rows in a table  (Stefan Weiss <spaceman@foo.at>)
Responses Re: Links between rows in a table  (Stefan Weiss <spaceman@foo.at>)
List pgsql-sql
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.


pgsql-sql by date:

Previous
From: bandeng
Date:
Subject: count array in postgresql
Next
From: Stefan Weiss
Date:
Subject: Re: Links between rows in a table