Re: Links between rows in a table

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Stefan Weiss <spaceman(at)foo(dot)at>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Links between rows in a table
Date: 2005-03-06 17:42:01
Message-ID: 20050306174201.GA19019@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sun, Mar 06, 2005 at 05:42:14 +0100,
Stefan Weiss <spaceman(at)foo(dot)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.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stefan Weiss 2005-03-06 18:27:38 Re: Links between rows in a table
Previous Message bandeng 2005-03-06 16:54:15 count array in postgresql