8000 Prevent creation of duplicate not-null constraints for domains · sthagen/postgres-postgres@647cffd · GitHub
[go: up one dir, main page]

Skip to content

Commit 647cffd

Browse files
Álvaro Herrerajianhe-fun
andcommitted
Prevent creation of duplicate not-null constraints for domains
This was previously harmless, but now that we create pg_constraint rows for those, duplicates are not welcome anymore. Backpatch to 18. Co-authored-by: jian he <jian.universality@gmail.com> Co-authored-by: Álvaro Herrera <alvherre@kurilemu.de> Discussion: https://postgr.es/m/CACJufxFSC0mcQ82bSk58sO-WJY4P-o4N6RD2M0D=DD_u_6EzdQ@mail.gmail.com
1 parent 87251e1 commit 647cffd

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/backend/commands/typecmds.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,11 +939,19 @@ DefineDomain(ParseState *pstate, CreateDomainStmt *stmt)
939939
break;
940940

941941
case CONSTR_NOTNULL:
942-
if (nullDefined && !typNotNull)
942+
if (nullDefined)
943+
{
944+
if (!typNotNull)
945+
ereport(ERROR,
946+
errcode(ERRCODE_SYNTAX_ERROR),
947+
errmsg("conflicting NULL/NOT NULL constraints"),
948+
parser_errposition(pstate, constr->location));
949+
943950
ereport(ERROR,
944-
errcode(ERRCODE_SYNTAX_ERROR),
945-
errmsg("conflicting NULL/NOT NULL constraints"),
951+
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
952+
errmsg("redundant NOT NULL constraint definition"),
946953
parser_errposition(pstate, constr->location));
954+
}
947955
if (constr->is_no_inherit)
948956
ereport(ERROR,
949957
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),

src/test/regress/expected/domain.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,11 @@ insert into domain_test values (1, 2);
10191019
-- should fail
10201020
alter table domain_test add column c str_domain;
10211021
ERROR: domain str_domain does not allow null values
1022+
-- disallow duplicated not-null constraints
1023+
create domain int_domain1 as int constraint nn1 not null constraint nn2 not null;
1024+
ERROR: redundant NOT NULL constraint definition
1025+
LINE 1: ...domain int_domain1 as int constraint nn1 not null constraint...
1026+
^
10221027
create domain str_domain2 as text check (value <> 'foo') default 'foo';
10231028
-- should fail
10241029
alter table domain_test add column d str_domain2;

src/test/regress/sql/domain.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ insert into domain_test values (1, 2);
602602
-- should fail
603603
alter table domain_test add column c str_domain;
604604

605+
-- disallow duplicated not-null constraints
606+
create domain int_domain1 as int constraint nn1 not null constraint nn2 not null;
607+
605608
create domain str_domain2 as text check (value <> 'foo') default 'foo';
606609

607610
-- should fail

0 commit comments

Comments
 (0)
0