提问人:vladlen32230 提问时间:9/26/2023 更新时间:9/26/2023 访问量:61
错误:正则表达式无效:postgresql中的字符范围无效
ERROR: invalid regular expression: invalid character range in postgresql
问:
尝试插入右行但不能,因为它违反了检查约束
桌子:
CREATE TABLE IF NOT EXISTS public.authors
(
au_id character varying(11) COLLATE pg_catalog."default" NOT NULL,
au_lname character varying(40) COLLATE pg_catalog."default" NOT NULL,
au_fname character varying(20) COLLATE pg_catalog."default" NOT NULL,
phone character(12) COLLATE pg_catalog."default" NOT NULL DEFAULT 'UNKNOWN'::bpchar,
address character varying(40) COLLATE pg_catalog."default",
city character varying(20) COLLATE pg_catalog."default",
state character(2) COLLATE pg_catalog."default",
zip character(5) COLLATE pg_catalog."default",
contract bit(1) NOT NULL,
CONSTRAINT authors_au_id_primary_key PRIMARY KEY (au_id),
CONSTRAINT authors_au_zip_check CHECK (zip ~ similar_to_escape('[0-9][0-9][0-9][0-
9][0-9]'::text)),
CONSTRAINT authors_au_id_check CHECK (au_id::text ~ similar_to_escape('[0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9][0-9][0-9]'::text))
)
插入:
INSERT into authors VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932', '6223 Bateman St.', 'Berkeley', 'CA', '94705', 1::bit);
错误: 错误:正则表达式无效:字符范围无效
SQL 状态:2201B
答:
0赞
Littlefoot
9/26/2023
#1
对我来说,看起来好像其中一个约束设置错误(因为正则表达式中间的 CRLF):
CONSTRAINT authors_au_zip_check CHECK (zip ~ similar_to_escape('[0-9][0-9][0-9][0-
9][0-9]'::text)),
如果是单行:
CONSTRAINT authors_au_zip_check CHECK (zip ~ similar_to_escape('[0-9][0-9][0-9][0-9][0-9]'::text)),
然后代码 - (包含) - 工作。看看小提琴。insert
0赞
MonkeyZeus
9/26/2023
#2
实际上没有理由弄乱范围。您可以将数字元转义与固定重复量词一起使用:
CONSTRAINT authors_au_zip_check CHECK (zip ~ similar_to_escape('\d{5}'::text)),
CONSTRAINT authors_au_id_check CHECK (au_id::text ~ similar_to_escape('\d{3}-\d{2}-\d{4}'::text))
评论