Re: foreign key with where clause

From: Branden Visser <mrvisser(at)gmail(dot)com>
To: Mark Lybarger <mlybarger(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: foreign key with where clause
Date: 2016-08-18 17:19:04
Message-ID: CAOo4Obrt=ZnpoF41Kq8BueL+HtZ5oQGhhzApLP2oaTBmbNsa-g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

My first instinct would be to look into triggers. In addition to an FK
a(b_id) -> b(id), you could have an insert and update trigger on
a(b_id) and b(active) to ensure the additional constraints.

On Thu, Aug 18, 2016 at 1:10 PM, Mark Lybarger <mlybarger(at)gmail(dot)com> wrote:
> I have two tables that i want to link with a FK where the child table record
> is "active".
>
> some googling shows that i could use a function and a check constraint on
> the function, but that only works for inserts, not updates on table b.
>
> create table a (int id, text name);
> create table b (int id, boolean active);
>
> alter table a add column b_id integer;
> -- how to do this?
> alter table a add foreign key (b_id) references b(id) where b.active == true
>
> help :).

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Manuel Gómez 2016-08-18 17:59:36 Re: foreign key with where clause
Previous Message Mark Lybarger 2016-08-18 17:10:16 foreign key with where clause