CREATE OR REPLACE FUNCTION fn_process_delete() RETURNS TRIGGER AS $$ BEGIN UPDATE workflow SET deleted_process_name = OLD.process_name WHERE process_id = OLD.process_id; RETURN NULL; END; $$LANGUAGE plpgsql; DROP TRIGGER IF EXISTS process_delete ON processes; CREATE TRIGGER process_delete AFTER DELETE ON processes FOR EACH ROW EXECUTE PROCEDURE fn_process_delete();
我的问题是双重的:
>如果我如上所述使用AFTER DELETE,则该行将被删除,但update语句不会更新“workflow”表中的字段.
>如果我使用BEFORE DELETE,则进程表将根本不执行删除,并发出错误消息“此行没有唯一标识符”.
任何人都可以建议吗?
您的触发器功能以:
RETURN NULL;
这样就可以跳过触发事件的执行. Per documentation on trigger procedures:
Row-level triggers fired
BEFORE
can return null to signal the trigger
manager to skip the rest of the operation for this row (i.e.,
subsequent triggers are not fired,and theINSERT
/UPDATE
/DELETE
does
not occur for this row).
您需要将其替换为:
RETURN OLD;
让系统继续删除行.原因如下:
In the case of a before-trigger on
DELETE
,the returned value has no
direct effect,but it has to be nonnull to allow the trigger action to
proceed. Note thatNEW
is null inDELETE
triggers,so returning that
is usually not sensible. The usual idiom inDELETE
triggers is to
returnOLD
.
大胆强调我的.
问题1
我认为你的触发器和触发器功能不应该像AFTER DELETE那样工作.毫无疑问,表工作流中必须存在具有匹配process_id的行.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。