我定义了一个函数,我想更新它被修改的记录值。
CREATE FUNCTION update_project_status_away_from_started()
RETURNS TRIGGER AS $$
BEGIN
IF OLD.status = 'started' THEN
NEW.status = 'updating';
RETURN NEW;
END IF;
RETURN OLD;
END;
$$ language 'plpgsql';
但是,当我更新一行时,出现以下错误:
ERROR: operator does not exist: projectstatus = character varying
枚举定义如下:
CREATE TYPE projectstatus AS ENUM ('started', 'updating', 'complete');
我对此的理解是,正在将枚举与字符串进行比较,但不知道该怎么做。不幸的是,我不知道如何将字符串 ('started') 转换为枚举。 http://www.postgresql.org/docs/9.1/static/datatype-enum.html 处的 postgresql 页面真的帮不了我太多。有人知道吗?
最佳答案
Postgres 提示你需要转换它:
IF OLD.status = 'started'::projectstatus THEN
NEW.status := 'updating'::projectstatus;
RETURN NEW;
END IF;
此外,正如 Igor 的回答中所述,最好使用 :=
进行赋值,而不是遗留(已弃用,但仍然有效)=
赋值运算符。
关于postgresql - 比较函数中的 postgresql 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16383886/