添加约束以更改域错误

ADD Constraint to ALTER DOMAIN ERROR

当我执行时:

ALTER DOMAIN public.remainingamount
    ADD CONSTRAINT "chk_remainingAmount" CHECK (remainingAmount > 0);

我在 pgAdmin 4 中收到以下消息:

ERROR: column "remainingamount" does not exist SQL state: 42703

该列存在。我可以在侧边栏中看到它!作为证据,当我执行以下行时:

ALTER DOMAIN public.remainingamount
    ADD CONSTRAINT "chk_remainingAmount" CHECK (remainingAmount > 0);

我收到消息:

ERROR: type "remainingamount" already exists SQL state: 42710

如何向现有域添加新约束?

您可以在 DOMAINCHECK 约束中使用的唯一变量是 VALUE。假设基本类型是 money:

ALTER DOMAIN public.remainingamount
    ADD CONSTRAINT "chk_remainingAmount" CHECK (VALUE > 0::money);
    

the documentation:

CHECK (expression)

(...)

Currently, CHECK expressions cannot contain subqueries nor refer to variables other than VALUE.