pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aaron Bono <postgresql@aranya.com>
To: pgsql-sql@postgresql.org
Subject: Re: query to select a linked list
Date: Wed, 9 May 2007 08:29:22 -0500
Message-ID: <bf05e51c0705090629l7232a6d2we87170b2168c1c82@mail.gmail.com> (raw)
In-Reply-To: <20070509125520.GA17497@apartia.fr>
References: <20070509125520.GA17497@apartia.fr>

On 5/9/07, Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
wrote:
>
> Hi,
>
> To build a threaded forum application I came up the following schema:
>
> forum
> ------
> id_forum | integer| not null  default
> nextval('forum_id_forum_seq'::regclass)
> id_parent| integer|
> subject  | text   | not null
> message  | text   |
>
> Each message a unique id_forum and an id_parent pointing to the replied
> post (empty if first post).
>
> How can I build an elegant query to select all messages in a thread?
>
> Thanks,
>

Unlike Oracle, PostgreSQL doesn't have anything like a connect by so you
would need to write your own stored procedure for that (if new versions of
PostgreSQL will have connect by, let me know guys).

What I did was add a little redundancy to my forum tables and had a table
structure kind of like this:

forum
   forum_id BIGSERIAL PK,
   name VARCHAR(50)

forum_topic
   forum_topic_id BIGSERIAL PK,
   forum_id BIGINT FK to forum

forum_post
   forum_post_id BIGSERIAL PK,
   create_dt TIMESTAMP,
   subject VARCHAR(255),
   message TEXT,
   forum_topic_id BIGINT FK to forum_topic

and if you want threading, you add a parent_forum_post_id to forum_post
(this is where you get the redundancy since only the top forum_post record
needs a reference to forum_topic and forum_topic wouldn't even really be
needed.

-Aaron


-- 
==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
==================================================================

view thread (12+ messages)  latest in thread

Message-ID: <bf05e51c0705090629l7232a6d2we87170b2168c1c82@mail.gmail.com>
Permalink:  ../bf05e51c0705090629l7232a6d2we87170b2168c1c82@mail.gmail.com/
Also on:    postgresql.org/message-id/bf05e51c0705090629l7232a6d2we87170b2168c1c82@mail.gmail.com

 · 

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: postgresql@aranya.com
  Subject: Re: query to select a linked list
  In-Reply-To: <bf05e51c0705090629l7232a6d2we87170b2168c1c82@mail.gmail.com>

* 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