ON DELETE SET NULL is a standard foreign key constraint option.
CREATE TABLE some_child ( parent_id integer references parent(id) on delete set null );
or
ALTER TABLE some_child ADD CONSTRAINT parent_id_fk FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE SET NULL;
See the documentation .
In future posts, be sure to include the PostgreSQL version and explain what you have already tried.
Craig Ringer
source share