· 8 years ago · Mar 17, 2018, 12:58 AM
1drop schema if exists X cascade;
2create schema X;
3
4create domain X.an_illegal_regex as text check ( value ~ '(' );
5
6create table X.table_with_illegal_constraint (
7 a text,
8 constraint "column a must have a bogus value" check ( a::X.an_illegal_regex = a ) );
9
10select * from X.table_with_illegal_constraint;
11
12insert into X.table_with_illegal_constraint values
13 ( 'xxx' ),
14 -- ( 'xxx' ),
15 ( 'foo' ),
16 ( 'xyx' );
17
18psql:db/experiments/pg-error-fail-illegal-regex.sql:17: ERROR:
19invalid regular expression: parentheses () not balanced
20
21drop schema if exists X cascade;
22create schema X;
23
24create domain X.a_legal_regex as text check ( value ~ '^x' );
25
26create table X.table_with_constraints (
27 a text,
28 constraint "column a must start with x" check ( a::X.a_legal_regex = a ),
29 constraint "field b must have 3 characters" check ( character_length( a ) = 3 ) );
30
31insert into X.table_with_constraints values
32 ( 'xxx' ),
33 ( 'foo' ), /* A: violates first constraint */
34 -- ( 'xxxx' ), /* B: violates second constraint */
35 ( 'xyx' );
36
37psql:db/experiments/pg-error-fail-no-constraint-name.sql:16:
38ERROR: new row for relation "table_with_constraints" violates
39check constraint "field b must have 3 characters"
40DETAIL: Failing row contains (xxxx).
41
42psql:db/experiments/pg-error-fail-no-constraint-name.sql:16:
43ERROR: value for domain x.a_legal_regex violates check constraint "a_legal_regex_check"