Re: foreign key to multiple tables depending on another column's value

From: Vick Khera <vivek(at)khera(dot)org>
To: Rodrigo Rosenfeld Rosas <rr(dot)rosas(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: foreign key to multiple tables depending on another column's value
Date: 2013-05-29 15:51:03
Message-ID: CALd+dcdyGn12Fbmyhpau6JvfB3MiiiYvXCH2WbFWFHoSN-P7Lg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

On Wed, May 29, 2013 at 9:58 AM, Rodrigo Rosenfeld Rosas <rr(dot)rosas(at)gmail(dot)com
> wrote:

> I know I could use a trigger, or some check constraint maybe, to ensure
> the field exists upon insert (or update), but I can't ensure the database
> will become inconsistent in case I remove a mapped field from the other
> schema.
>
> Now I can finally explain my question: is it possible that I set some sort
> of foreign key whose referenced table and column would depend on the value
> of another column?
>

The FK tests are basically triggers, but highly optimized.

That said, the way they enforce the integrity is by having a trigger on
both tables. So for your custom need here, you would want to put a trigger
on the referenced table to disallow deleting a value that is still
referenced, or do whatever appropriate action upon delete/update your
application needs.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rodrigo Rosenfeld Rosas 2013-05-29 16:14:00 Re: [GENERAL] foreign key to multiple tables depending on another column's value
Previous Message Bruce Momjian 2013-05-29 15:17:24 Re: pg_upgrade -u

Browse pgsql-sql by date

  From Date Subject
Next Message Rodrigo Rosenfeld Rosas 2013-05-29 16:14:00 Re: [GENERAL] foreign key to multiple tables depending on another column's value
Previous Message Marc Mamin 2013-05-29 15:07:07 Re: reduce many loosely related rows down to one