Is it possible to only allow deletes from a table via referential integrity cascades? - Mailing list pgsql-sql

From Christopher Maier
Subject Is it possible to only allow deletes from a table via referential integrity cascades?
Date
Msg-id 8F55A6E9-8A56-4261-B35A-5A7012D2C8CA@med.unc.edu
Whole thread Raw
List pgsql-sql
I have two tables joined by a foreign key constraint:

> CREATE TABLE test_master(
>     id SERIAL PRIMARY KEY,
>     foo TEXT
> );
>
> CREATE TABLE test_detail(
>     id SERIAL PRIMARY KEY,
>     master BIGINT NOT NULL REFERENCES test_master(id) ON DELETE  
> CASCADE ON UPDATE CASCADE,
>     bar TEXT
> );

Is there a way to block deletes on the "test_detail" table that will  
only allow rows to be deleted if it is the result of deleting the  
corresponding "test_master" record?  In other words, I'd like to  
disallow direct DELETE commands like this:

> DELETE FROM test_detail WHERE id = 1;

while allowing a command like

> DELETE FROM test_master WHERE id = 1;

to subsequently delete via CASCADE all "test_detail" rows that  
reference test_master # 1.

I've tried using rules and revoking privileges, but both of these  
approaches fail when trying to delete from "test_master".

Thanks in advance,

Chris



pgsql-sql by date:

Previous
From: "Oliveiros Cristina"
Date:
Subject: Re: Best way to "and" from a one-to-many joined table?
Next
From: "Allan Kamau"
Date:
Subject: Re: Collapsing (select) row values into single text field.