Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCDlG-00068Y-3g for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 02:07:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iCDlE-0002oN-Rf for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 02:07:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCDlE-0002np-D7 for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2019 02:07:32 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iCDlC-0004w7-5W for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2019 02:07:31 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d3so5841476plr.1 for ; Sun, 22 Sep 2019 19:07:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:organization:references:date:in-reply-to :message-id:user-agent:mime-version; bh=2YCXwNc+WrGOVWKUftGnkaw2bMpumRK3GsD/Z6UYZKY=; b=h6F8wtskpqbJwPXcesf7jm58q1ro1fS/Ik5xqUZZFpKtHNrWhukRavpQGm+r0yAy0h FWYTReYvyeBd00coQpu2M43A/etvfOsUM5+Ka+c+L5/AguJg10Lz4ZA6U/aC3HAr9UhR CY3rxB4KrWGvzcddc6u3tnR0i353ElcWvGVSZeFwCXm9NGI4PSFjPI8q9jaY+k1pXW7d 7iY952ueLe4+/f65IUFmX0SPwZdtOP1lwv0aWCpnqsB+po4vxAEw/NaCA3F9fKZisWfe 7DhuvLspcBGk7Moi2Z18HTlmQ9bcLV47C7C+PFYKFHrDdEUPqqa+sYqepMFDOrCbXjvt U9yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:organization:references:date :in-reply-to:message-id:user-agent:mime-version; bh=2YCXwNc+WrGOVWKUftGnkaw2bMpumRK3GsD/Z6UYZKY=; b=UOwUrygr0yVTCSDpzNZk7mZoME23E3lTnQC6g5rDewayvi5kqFjyZ174NXrnMKFgws MTOvvdVHFjDDhSBaXhWLiQ54NvvMUPcB/hjtIdB01KnSzjwHu5co66YWWKEb1GxQG7Pk lYZMvWphph3YCtsc4Q5SvcHxfRBfFS9UznPqa4WjHbp+4zp5wbiVt+gzvUX+SRGJnDZw 2VHRbJj4l9Vla0nRpssR83Dxy6G2CIw9bYTNLT8bR95by4eKwg2FbRQLkcba/wO9TRuZ aiLrCqJ+7MkxHKYAa7ghWn/pw7AFJ1NElVaHvy/gFmLXeAMQ65YcKfnbFt5y9Pq3Z9XA gM7w== X-Gm-Message-State: APjAAAVsp2Px8d/6zPGDerAgdNRgp28tRxf9hNrB7d2OQ8eSVd158d+m Wr0PeoyE5H1vspcR2rpBNELgPFU= X-Google-Smtp-Source: APXvYqxlRTQqyD6xTpJ0uUwFNQrlxMZnhDWTaxG0b1NPLOExxBlWIxkXeMqeFWr7wieHN7HfNO3lZg== X-Received: by 2002:a17:902:7244:: with SMTP id c4mr29216604pll.178.1569204448644; Sun, 22 Sep 2019 19:07:28 -0700 (PDT) Received: from Pankajs-MacBook-Pro.local ([49.36.128.80]) by smtp.gmail.com with ESMTPSA id u10sm7909513pfh.61.2019.09.22.19.07.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Sep 2019 19:07:27 -0700 (PDT) Received: by Pankajs-MacBook-Pro.local (Postfix, from userid 501) id 7F0652039082D1; Mon, 23 Sep 2019 07:37:22 +0530 (IST) From: Pankaj Jangid To: Francisco Olarte Cc: Pankaj Jangid , "pgsql-generallists.postgresql.org" Subject: Re: How to represent a bi-directional list in db? Organization: Code is Great! References: Date: Mon, 23 Sep 2019 07:37:22 +0530 In-Reply-To: (Francisco Olarte's message of "Sun, 22 Sep 2019 17:39:11 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (darwin) MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Francisco Olarte writes: > On Sun, Sep 22, 2019 at 4:25 PM Pankaj Jangid 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