Suppose I have the following tables
CREATE TABLE plugins (
id int primary key,
type text);
insert into plugins values (1,\'matrix\');
insert into plugins va
You can use a CHECK constraint for this. You can't put a query in a CHECK constraint but you can call a function; so, we build a simple function that tells us if a pluginid is a matrix:
create or replace function is_matrix(int) returns boolean as $$
select exists (
select 1
from plugins
where id = $1
and type = 'matrix'
);
$$ language sql;
and wrap that in a CHECK constraint:
alter table matrix_params add constraint chk_is_matrix check (is_matrix(pluginid));
Then:
=> insert into matrix_params values (1,1);
=> insert into matrix_params values (2,3);
ERROR: new row for relation "matrix_params" violates check constraint "chk_is_matrix"
And the FK takes care of referential integrity and cascades.