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

DROP TRIGGER delete_filepricemodel_trigger ON filepricemodel;
CREATE TRIGGER delete_filepricemodel_trigger AFTER UPDATE OF deleted ON filepricemodel FOR EACH ROW EXECUTE PROCEDURE remove_filepricemodel_id();

