Re: foreign key with where clause - Mailing list pgsql-general

From Branden Visser
Subject Re: foreign key with where clause
Date
Msg-id CAOo4Obrt=ZnpoF41Kq8BueL+HtZ5oQGhhzApLP2oaTBmbNsa-g@mail.gmail.com
Whole thread Raw
In response to foreign key with where clause  (Mark Lybarger <mlybarger@gmail.com>)
List 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@gmail.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 :).


pgsql-general by date:

Previous
From: Mark Lybarger
Date:
Subject: foreign key with where clause
Next
From: Manuel Gómez
Date:
Subject: Re: foreign key with where clause