pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pankaj Jangid <pankaj.jangid@gmail.com>
To: Francisco Olarte <folarte@peoplecall.com>
Cc: Pankaj Jangid <pankaj.jangid@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: How to represent a bi-directional list in db?
Date: Mon, 23 Sep 2019 07:37:22 +0530
Message-ID: <m2lfufhijp.fsf@gmail.com> (raw)
In-Reply-To: <CA+bJJbzL4Z64TTPpaiAdTBnuTCqnpcjft=PiNrd3NiyQ8Edf1w@mail.gmail.com>
References: <m2muew8l37.fsf@gmail.com>
	<CA+bJJbzL4Z64TTPpaiAdTBnuTCqnpcjft=PiNrd3NiyQ8Edf1w@mail.gmail.com>

Francisco Olarte <folarte@peoplecall.com> writes:

> On Sun, Sep 22, 2019 at 4:25 PM Pankaj Jangid <pankaj.jangid@gmail.com> wrote:
>> CREATE TABLE stages (
>>        id SERIAL PRIMARY KEY,
>>        name  VARCHAR(80) NOT NULL,
>>        created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
>>        updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
>>        prev_stage_id SERIAL REFERENCES stages NULL,
>>        next_stage_id SERIAL REFERENCES stages NULL,
>>        process_id SERIAL REFERENCES processes NOT NULL
>> );
>> Failed with: conflicting NULL/NOT NULL declarations for column
>> "prev_stage_id" of table "stages"
>> Is it not possible to create "nullable" self referencing foreign keys?
>
> Serial seems wrong. It means integer, not null, defaul next value from
> a sequence.
>
> What you probably want is just "prev_stage_id INTEGER" ( NULL by
> default ), as you do not want the prev/next stage ids to be generated,
> you normally would want to assign values from other tuples.
>

Thanks. This resolved my problem of NULL/NOT NULL conflict. I wasn't
aware that SERIAL is by default NOT NULL.

> Also, you may have problems populating this kind of table, as you will
> not have the ids from either prev or next stage when building it.
>

If NULL value is allowed I can fill it up with NULL initially. Right? Or
is there something wrong here.

> And lastly, in SQL you do not really need a doubly linked list, just
> populate prev_stage_id, and index it and you can query next stage of a
> tuple using it.
>

Could you please elaborate? Suppose I have this table,

CREATE TABLE stages (
    id SERIAL PRIMARY KEY,
    name VARCHAR(80) NOT NULL,
    next_id INTEGER REFERENCE stages NULL,
);

What would be the backward query in that case? Forward is clear. This is
forward query,

SELECT name FROM stages WHERE next_id = 123;

-- 
Pankaj Jangid





view thread (7+ messages)  latest in thread

Message-ID: <m2lfufhijp.fsf@gmail.com>
Permalink:  ../m2lfufhijp.fsf@gmail.com/
Also on:    postgresql.org/message-id/m2lfufhijp.fsf@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pankaj.jangid@gmail.com, folarte@peoplecall.com
  Subject: Re: How to represent a bi-directional list in db?
  In-Reply-To: <m2lfufhijp.fsf@gmail.com>
  References: <m2muew8l37.fsf@gmail.com> <CA+bJJbzL4Z64TTPpaiAdTBnuTCqnpcjft=PiNrd3NiyQ8Edf1w@mail.gmail.com> <m2lfufhijp.fsf@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