CREATE OR REPLACE FUNCTION remove_filelicense_id() RETURNS trigger AS '
 BEGIN
  UPDATE filelicense_details SET deleted = new.deleted WHERE filelicenseid = new.id;
  IF new.deleted = TRUE THEN
    UPDATE file SET filelicenseid = NULL WHERE filelicenseid = new.id;
  END IF;
  RETURN NULL;
 END
' LANGUAGE plpgsql;

DROP TRIGGER delete_filelicense_id_trigger ON filelicense;
CREATE TRIGGER delete_filelicense_id_trigger AFTER UPDATE OF deleted ON filelicense FOR EACH ROW EXECUTE PROCEDURE remove_filelicense_id();

