Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id D2E3A9FB6DE for ; Wed, 9 May 2007 10:29:30 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 33098-08 for ; Wed, 9 May 2007 10:29:18 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.243]) by postgresql.org (Postfix) with ESMTP id 347149FB5BC for ; Wed, 9 May 2007 10:29:24 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id d17so45440and for ; Wed, 09 May 2007 06:29:23 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=tHTqFWEuec+eJfc9r7j9UvxoOIEGF3pk3guQCS0LbUg6hLeaCcVhwhyPCMzvHDqie388WOQ4TMUi+fvYisRRJYJPxC3zRQu1G6DpRVqcYWbMNvVaFMd9byVFJc7Iy9HZl/Qb6Q1eTpNMZFvAdzF1uIPs54J5EuJbrJv2QHIFkLc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=KVOZNlOQbiP9suBjHQeWOXk/9UblfKxTaShjYG1H6o6Q5C0++wTjLl6PYcZX0IUl+OvbzwITU0BWs0bfn0UOxypfdNNMgTas3140rwiWeKYaa5XYiPnVmZm3vtXbmWoMlKZRqP8H5tCcnFB/2wMtv31BwTanqYgKF3AueXie+II= Received: by 10.100.139.9 with SMTP id m9mr343145and.1178717362417; Wed, 09 May 2007 06:29:22 -0700 (PDT) Received: by 10.100.144.18 with HTTP; Wed, 9 May 2007 06:29:22 -0700 (PDT) Message-ID: Date: Wed, 9 May 2007 08:29:22 -0500 From: "Aaron Bono" To: pgsql-sql@postgresql.org Subject: Re: query to select a linked list In-Reply-To: <20070509125520.GA17497@apartia.fr> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_134986_20752045.1178717362361" References: <20070509125520.GA17497@apartia.fr> X-Google-Sender-Auth: 11b39c621407ab8d X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/67 X-Sequence-Number: 28347 ------=_Part_134986_20752045.1178717362361 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/9/07, Louis-David Mitterrand 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 ================================================================== ------=_Part_134986_20752045.1178717362361 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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
================================================================== ------=_Part_134986_20752045.1178717362361--