Re: Foreign keys on array elements

From: Pavel Stehule <stehule(at)kix(dot)fsv(dot)cvut(dot)cz>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Foreign keys on array elements
Date: 2005-04-20 07:31:34
Message-ID: Pine.LNX.4.44.0504200923250.9405-100000@kix.fsv.cvut.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


> Hi,
>
> Can you put a foreign key constraint on an array column that says that
> each element of the array must match a primary key?
>
> If not, is this a TODO perhaps?
>
> Chris
>
Hello,

Using array values for foreign key is very special. I not sure, so all
people need it. More interesting is CHECK on array. But you can write
simply trigger.

CREATE OR REPLACE FUNCTION check_() RETURNS TRIGGER AS $$
DECLARE _v integer;
BEGIN
FOR _i IN array_lower(NEW.array_value,1) ..
array_upper(NEW.array_value,1)
LOOP
PERFORM 1 FROM some_tab WHERE pk = NEW.array_value[_i];
IF NOT FOUND THEN
RAISE EXCEPTION '..........';
END IF;
END LOOP;
RETURN NEW;
END; $$ LANGUAGE plpgsql;

CREATE TRIGGER foo BEFORE INSERT OR UPDATE ON ...
FOR EACH ROW EXECUTE PROCEDURE check_();

Regards
Pavel Stehule

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andreas Pflug 2005-04-20 11:05:13 Re: [GENERAL] Idea for the statistics collector
Previous Message Klaus Naumann 2005-04-20 07:28:26 Re: Problem with PITR recovery