From: hubert depesz lubaczewski <depesz@depesz.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Mai Peng <maily.peng@webedia-group.com>
Cc: Pgsql-admin <pgsql-admin@lists.postgresql.org>
Subject: Re: Design database
Date: Wed, 3 Feb 2021 17:44:59 +0100
Message-ID: <20210203164459.GA17357@depesz.com> (raw)
In-Reply-To: <CAKFQuwYjH8proEfMJ7jnFo9k_6tABgOG=ib5TeEnTSjGMQmPqQ@mail.gmail.com>
References: <FDD700F1-0FC8-4242-9C55-9D2CC590F71D@webedia-group.com>
<CAKFQuwYjH8proEfMJ7jnFo9k_6tABgOG=ib5TeEnTSjGMQmPqQ@mail.gmail.com>
On Wed, Feb 03, 2021 at 08:35:41AM -0700, David G. Johnston wrote:
> On Wed, Feb 3, 2021 at 8:23 AM Mai Peng <[1]maily.peng@webedia-group.com> wrote:
>
> Hello,
>
> I’ve got a table name publication, and another table named publication_item.
> Each publication_item is linked to a publication by publication_id.
> How could I add a constraint that check each publication has one or more publication_item when a tuple is inserted ?
> I don’t want to store items in the same table as publication because we could have 1-> n items.
>
> [2]https://www.postgresql.org/docs/current/tutorial-fk.html
I don't think it helps. Fkey can check that publication_item contains
valid publication_id, but doesn't ensure that every publication has at
least one item.
As for the problem of OP, I think this describes working solution:
https://www.depesz.com/2017/07/03/how-to-make-sure-there-is-always-at-least-one-sub-record/
Best regards,
depesz
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-admin@postgresql.org
Cc: depesz@depesz.com, david.g.johnston@gmail.com, maily.peng@webedia-group.com, pgsql-admin@lists.postgresql.org
Subject: Re: Design database
In-Reply-To: <20210203164459.GA17357@depesz.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