设置(仅在 PostgreSQL 9.6 上尝试过):
CREATE TYPE MY_ENUM AS ENUM ('a', 'b');
CREATE CAST (CHARACTER VARYING AS MY_ENUM) WITH INOUT AS IMPLICIT;
CREATE TABLE t (x MY_ENUM);
INSERT INTO t VALUES ('a'::MY_ENUM), ('b'::MY_ENUM);
正如预期的那样,这些工作正常:
INSERT INTO t VALUES ('a');
SELECT * FROM t WHERE x = 'a';
但这些不会:
PREPARE p(CHARACTER VARYING) AS SELECT * FROM t WHERE x = $1;
;; error: operator does not exist: my_enum = character varying
CREATE FUNCTION f(ix CHARACTER VARYING, OUT ox MY_ENUM) AS
$$
SELECT * FROM t WHERE x = ix
$$ LANGUAGE sql;
;; error: operator does not exist: my_enum = character varying
CREATE FUNCTION f(ix CHARACTER VARYING) RETURNS VOID AS
$$
BEGIN
SELECT * FROM t WHERE input_type = ix;
END;
$$ LANGUAGE plpgsql;
SELECT f('piano');
;; error: operator does not exist: my_enum = character varying
根据文档:
如果强制转换标记为 AS
IMPLICIT
,则可以在任何上下文中隐式调用它,无论是赋值还是在表达式内部。
那么为什么会出现错误呢?