mirror of
https://github.com/postgres/postgres.git
synced 2025-07-31 22:04:40 +03:00
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
This commit is contained in:
@ -939,11 +939,19 @@ DefineDomain(ParseState *pstate, CreateDomainStmt *stmt)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case CONSTR_NOTNULL:
|
case CONSTR_NOTNULL:
|
||||||
if (nullDefined && !typNotNull)
|
if (nullDefined)
|
||||||
|
{
|
||||||
|
if (!typNotNull)
|
||||||
|
ereport(ERROR,
|
||||||
|
errcode(ERRCODE_SYNTAX_ERROR),
|
||||||
|
errmsg("conflicting NULL/NOT NULL constraints"),
|
||||||
|
parser_errposition(pstate, constr->location));
|
||||||
|
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
errcode(ERRCODE_SYNTAX_ERROR),
|
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||||
errmsg("conflicting NULL/NOT NULL constraints"),
|
errmsg("redundant NOT NULL constraint definition"),
|
||||||
parser_errposition(pstate, constr->location));
|
parser_errposition(pstate, constr->location));
|
||||||
|
}
|
||||||
if (constr->is_no_inherit)
|
if (constr->is_no_inherit)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||||
|
@ -1019,6 +1019,11 @@ insert into domain_test values (1, 2);
|
|||||||
-- should fail
|
-- should fail
|
||||||
alter table domain_test add column c str_domain;
|
alter table domain_test add column c str_domain;
|
||||||
ERROR: domain str_domain does not allow null values
|
ERROR: domain str_domain does not allow null values
|
||||||
|
-- disallow duplicated not-null constraints
|
||||||
|
create domain int_domain1 as int constraint nn1 not null constraint nn2 not null;
|
||||||
|
ERROR: redundant NOT NULL constraint definition
|
||||||
|
LINE 1: ...domain int_domain1 as int constraint nn1 not null constraint...
|
||||||
|
^
|
||||||
create domain str_domain2 as text check (value <> 'foo') default 'foo';
|
create domain str_domain2 as text check (value <> 'foo') default 'foo';
|
||||||
-- should fail
|
-- should fail
|
||||||
alter table domain_test add column d str_domain2;
|
alter table domain_test add column d str_domain2;
|
||||||
|
@ -602,6 +602,9 @@ insert into domain_test values (1, 2);
|
|||||||
-- should fail
|
-- should fail
|
||||||
alter table domain_test add column c str_domain;
|
alter table domain_test add column c str_domain;
|
||||||
|
|
||||||
|
-- disallow duplicated not-null constraints
|
||||||
|
create domain int_domain1 as int constraint nn1 not null constraint nn2 not null;
|
||||||
|
|
||||||
create domain str_domain2 as text check (value <> 'foo') default 'foo';
|
create domain str_domain2 as text check (value <> 'foo') default 'foo';
|
||||||
|
|
||||||
-- should fail
|
-- should fail
|
||||||
|
Reference in New Issue
Block a user