我已经定义在PostgreSQL的9.3触发器,检查的attribute_group_attribute.default_value
长度的基础上attribute_type.max_length
值:PostgeSQL触发引发异常工作在INSERT但不能在UPDATE
CREATE OR REPLACE FUNCTION trigger_function()
RETURNS trigger AS
$BODY$
DECLARE
is_longer_than_max_length BOOLEAN;
BEGIN
IF (NEW.default_value IS NOT NULL) THEN
SELECT length(NEW.default_value) > attribute_type.max_length
INTO is_longer_than_max_length FROM attribute_group_attribute
JOIN attribute ON NEW.attribute_id = attribute.id JOIN attribute_type
ON attribute.attribute_type_id = attribute_type.id;
IF (is_longer_than_max_length) THEN
RAISE EXCEPTION 'Attribute type with id % has max length of %,
while attribute default value % has a length of %.',
attribute_type.id, attribute_type.max_length, NEW.default_value,
length(NEW.default_value) USING ERRCODE='20808';
END IF;
END IF;
RETURN NEW;
END
$BODY$
LANGUAGE 'plpgsql' SECURITY INVOKER
;
CREATE TRIGGER trigger_name BEFORE INSERT OR UPDATE ON attribute_group_attribute
FOR EACH ROW EXECUTE PROCEDURE trigger_function()
;
触发工作正常上INSERT
和加薪EXCEPTION
。但在UPDATE
我得到这个错误:
ERROR: missing FROM-clause entry for table "attribute_type" at character 8
QUERY: SELECT attribute_type.id
CONTEXT: PL/pgSQL function trg_func_attr_group_attr_attr_type() line 8 at RAISE