pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: Stefan Weiss <spaceman@foo.at>
Cc: pgsql-sql@postgresql.org
Subject: Re: Links between rows in a table
Date: Sun, 6 Mar 2005 11:42:01 -0600
Message-ID: <20050306174201.GA19019@wolff.to> (raw)
In-Reply-To: <422A8A26.1070704@foo.at>
References: <422A8A26.1070704@foo.at>

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.



view thread (7+ messages)  latest in thread

Message-ID: <20050306174201.GA19019@wolff.to>
Permalink:  ../20050306174201.GA19019@wolff.to/
Also on:    postgresql.org/message-id/20050306174201.GA19019@wolff.to

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bruno@wolff.to, spaceman@foo.at
  Subject: Re: Links between rows in a table
  In-Reply-To: <20050306174201.GA19019@wolff.to>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox