Re: Design database

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Mai Peng <maily(dot)peng(at)webedia-group(dot)com>, Pgsql-admin <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Re: Design database
Date: 2021-02-03 16:44:59
Message-ID: 20210203164459.GA17357@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

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(dot)peng(at)webedia-group(dot)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

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message dbatoCloud Solution 2021-02-04 13:47:48 Partitioning existing table issue - Help needed!
Previous Message Alvaro Herrera 2021-02-03 16:10:49 Re: pg_wal keeps growing