pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Array of foreign key
Date: Sat, 23 Dec 2017 20:25:05 +0100
Message-ID: <20171223192505.t3vinsfzesbhy735@hjp.at> (raw)
In-Reply-To: <CANu8FiwBDoRLAKndAMZQZNMMstFf0NrG91GRo44cK=aMeyDHag@mail.gmail.com>
References: <CAN_ctnhQ5heFbg9mgypUR3TrAQ4WOogiK2Yr9zHJssbHSKAQqw@mail.gmail.com>
	<CAN_ctniwPUFECRuLJ1Ui32Ci-7Wfo=xXN7z5eT=-8kMSkxhp0A@mail.gmail.com>
	<CAN_ctnhXbgZjArDZVLk-UNAvYiVEUXnGf+TG3kkvsD1edRB8Tw@mail.gmail.com>
	<CAN_ctnjC_N0NvB_a-NDCrArApukumhK5C2M2w3n8T=+pVRU6UQ@mail.gmail.com>
	<5FEA34BC-1AF4-4B31-BE9B-2A6CB1D8CADC@a-kretschmer.de>
	<CAN_ctniZMtuMPy++GNV_4w3FT+LWvXMWxSSzsq6Wm82MAT-n5g@mail.gmail.com>
	<CANu8Fix5=6ZCusDfVHPkGt4x1fxKDk-v+wjsr6oaJt8T+iCx4A@mail.gmail.com>
	<B4846243-6401-4BF0-828C-BB3906DA423C@a-kretschmer.de>
	<20171223170900.jzr33jx4d37yqo2j@hjp.at>
	<CANu8FiwBDoRLAKndAMZQZNMMstFf0NrG91GRo44cK=aMeyDHag@mail.gmail.com>

On 2017-12-23 12:24:07 -0500, Melvin Davidson wrote:
> On Sat, Dec 23, 2017 at 12:09 PM, Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
> >...Is there a way to
> >enforce foreign key constraints on the members of an array?
> >At insert time you can check with a trigger of course, and maybe there
> >is a way to do it in a check constraint
> 
> I don't think you understand how Foreign Key constraints work in PostgreSQL.
> PostgreSQL will prevent any insert where the value of a column is not within
> the FK table.
> So you DO NOT need a check constraint or trigger.
> 
> What I do not understand is your reference to a FK "array".

If you do not understand something, please ask. Don't claim that other
people "don't understand how X works" just because you don't know what
they are talking about. Also, please pay a bit of attention who you
are replying to. I am not the OP. I just understand what he wants (or at
least I think I do).

> So for the sake of CLARIDICATION, would you please
> 1, State the version of PostgreSQL
> 2. State the O/S

Why should I? You didn't state the OS and PostgreSQL version you use
either. And I don't think you should, as it is irrelevant for the
discussion.

> 3. Provide an example of an FK "array" that you are concerned with.

I think the OP wants something like this:

create table features (
    id serial primary key,
    name varchar not null
);

create table products (
    id serial primary key,
    name varchar not null,
    array_of_features int[]
        references [] features(id) -- XXX - invented syntax
);

where products.array_of_features can only contain integers from
features.id. This doesn't currently (as of 10.1) work.
As I wrote, you could use triggers, but there is no way to declare that
foreign key directly.

If you want foreign keys, you have to use an intersection table:

create table features (
    id serial primary key,
    name varchar not null
);

create table products (
    id serial primary key,
    name varchar not null
);

create table product_features (
    product references products(id),
    feature references features(id)
);

But that has a relatively high overhead both in storage and for queries.

I can understand why the OP wants that. I could have used something like
this in the past, too.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp@hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/;

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (17+ messages)  latest in thread

Message-ID: <20171223192505.t3vinsfzesbhy735@hjp.at>
Permalink:  ../20171223192505.t3vinsfzesbhy735@hjp.at/
Also on:    postgresql.org/message-id/20171223192505.t3vinsfzesbhy735@hjp.at

 · 

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: hjp-pgsql@hjp.at
  Subject: Re: Array of foreign key
  In-Reply-To: <20171223192505.t3vinsfzesbhy735@hjp.at>
  References: <CAN_ctnhQ5heFbg9mgypUR3TrAQ4WOogiK2Yr9zHJssbHSKAQqw@mail.gmail.com> <CAN_ctniwPUFECRuLJ1Ui32Ci-7Wfo=xXN7z5eT=-8kMSkxhp0A@mail.gmail.com> <CAN_ctnhXbgZjArDZVLk-UNAvYiVEUXnGf+TG3kkvsD1edRB8Tw@mail.gmail.com> <CAN_ctnjC_N0NvB_a-NDCrArApukumhK5C2M2w3n8T=+pVRU6UQ@mail.gmail.com> <5FEA34BC-1AF4-4B31-BE9B-2A6CB1D8CADC@a-kretschmer.de> <CAN_ctniZMtuMPy++GNV_4w3FT+LWvXMWxSSzsq6Wm82MAT-n5g@mail.gmail.com> <CANu8Fix5=6ZCusDfVHPkGt4x1fxKDk-v+wjsr6oaJt8T+iCx4A@mail.gmail.com> <B4846243-6401-4BF0-828C-BB3906DA423C@a-kretschmer.de> <20171223170900.jzr33jx4d37yqo2j@hjp.at> <CANu8FiwBDoRLAKndAMZQZNMMstFf0NrG91GRo44cK=aMeyDHag@mail.gmail.com> <20171223192505.t3vinsfzesbhy735@hjp.at>

* 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