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