Postgres:仅检查 INSERT 的约束 [英] Postgres: Constraint with check on INSERT only
本文介绍了Postgres:仅检查 INSERT 的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 Postgres 中设置一个约束,它只会在 INSERT 查询上触发(而不是在 UPDATE 查询上).
I need a constraint in Postgres which would be triggered on INSERT queriesonly (not on UPDATE queries).
目前它是这样实现的:
ALTER TABLE test ADD CONSTRAINT check_something_const
CHECK (check_something(id, task_id)) NOT VALID;
哪里check_something
CREATE OR REPLACE FUNCTION check_something(UUID, UUID)
RETURNS BOOLEAN AS $$
DECLARE
existed_id UUID;
BEGIN
SELECT id
INTO existed_id
FROM test WHERE ...etc.
RETURN existed_id IS NULL
END;
$$ LANGUAGE 'plpgsql' VOLATILE;
目前 check_something_const
在更新和插入查询时都会被触发.我需要的是在插入新行但不更新时仅检查此项.
And currently check_something_const
is triggered both on update and insert queries.
What I need is this to be checked only when new rows are inserted, but not updated.
推荐答案
这可以通过仅在插入时触发的触发器过程来实现.
This can be achieved with a trigger procedure that fires on insert only.
这篇关于Postgres:仅检查 INSERT 的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文