Re: Foreign key constraint for array-field?

From: Decibel! <decibel(at)decibel(dot)org>
To: Simon Riggs <simon(at)2ndQuadrant(dot)com>
Cc: dmitry(at)koterov(dot)ru, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Foreign key constraint for array-field?
Date: 2008-10-06 16:29:51
Message-ID: CFD90FD8-EAA1-468E-9E81-284A70C00BB0@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sep 21, 2008, at 4:18 AM, Simon Riggs wrote:
> On Sun, 2008-09-21 at 04:38 +0400, Dmitry Koterov wrote:
>
>> Is it possible to create a foreign key constraint for ALL elements of
>> an array field?
>>
>> CREATE TABLE a(id INTEGER);
>> CREATE TABLE b(id INTEGER, a_ids INTEGER[]);
>>
>> Field b.a_ids contains a list of ID's of "a" table. I want to ensure
>> that each element in b.a_ids exists in a in any time. Is it possible
>> to create an automatic foreign key?
>
> No, its not possible. Need a trigger.
>
> I think we should support it though. If we extend the relational model
> with arrays then it would be sensible if we support this aspect as
> well.

+1. And for everyone who immediately jumped to "NORMALIZE!" as the
answer, consider that that means a bare minimum of 24 bytes overhead
per item that would go into the array. It's not hard at all for that
overhead to become massive.
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Smith 2008-10-06 17:02:39 Re: Shouldn't pg_settings.enumvals be array of text?
Previous Message Decibel! 2008-10-06 16:17:35 Re: Ad-hoc table type?