Re: foreign key with where clause

From: Manuel Gómez <targen(at)gmail(dot)com>
To: Mark Lybarger <mlybarger(at)gmail(dot)com>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: foreign key with where clause
Date: 2016-08-18 17:59:36
Message-ID: CAJWnFaO8rK=Grg6RVHMNTP07EhBT57=u44UJcYams6opEU9qTg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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 :).

If you can afford the overhead, this works:

create table b (
id int primary key,
active boolean not null,
unique (id, active)
);

create table a (
id int primary key,
name text,
b_id int not null,
b_active boolean not null check (b_active = true),
foreign key (b_id, b_active) references b (id, active)
);

(The «= true» is redundant, of course)

Sadly, you need the extra redundant index on (id, active) despite
«active» being functionally dependent on «id», as foreign key
constraints require a unique index on the referenced table to the
exact set of columns that comprise the foreign key. You also need the
extra column in the referencing table, and it will only ever contain
«true».

It’d be very nice if foreign key constraints were aware of functional
dependencies, and even nicer if they could be pointed at a partial
index. This would allow a lot of complex integrity constraints to be
enforced with foreign key constraints with no overhead. Not sure how
feasible such a feature would be.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alexander M. Sauer-Budge 2016-08-18 18:02:20 Restrict CREATEROLE privilege grant to NOLOGIN only?
Previous Message Branden Visser 2016-08-18 17:19:04 Re: foreign key with where clause