无法检查域约束中的空文本

Can't check for empty text in domain constraint

我有一个这样的域名

create domain foo as text                                                   
     constraint not_blank check (char_length(value) > 0)                      
     constraint min_length check (char_length(value) > 1)                
     constraint suggested_length check (char_length(value) < 8)                                                                                                                                                   
     constraint max_length check (char_length(value) < 10);      

我也是这样测试的

 select * from length (''::foo);
 select * from length ('1'::foo);
 select * from length ('12345678'::foo);
 select * from length ('1234567890'::foo);

为什么我正确捕捉到建议长度与最大长度,但我无法正确捕捉到空白输入?

test=#  select * from length (''::foo);
ERROR:  value for domain foo violates check constraint "min_length"
test=#  select * from length ('1'::foo);
ERROR:  value for domain foo violates check constraint "min_length"
test=#  select * from length ('12345678'::foo);
ERROR:  value for domain foo violates check constraint "suggested_length"
test=#  select * from length ('1234567890'::foo);
ERROR:  value for domain foo violates check constraint "max_length"

空字符串违反了前三个检查约束,因此在错误中报告哪些约束是随机的。

您知道这些约束相互矛盾,对吗?

如果您希望按特定顺序测试多个条件,我建议使用触发器依次测试条件并抛出适当的异常。