diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2001-07-04 17:36:54 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2001-07-04 17:36:54 +0000 |
commit | 499c81d3a3ffef4645b720ace5909f00609bff79 (patch) | |
tree | 76c9321a8091df9027e47583f6f7fdb2f1b5d1b3 | |
parent | 5621ec0629b1da42869375210b2a90e6605dd9f0 (diff) | |
download | postgresql-499c81d3a3ffef4645b720ace5909f00609bff79.tar.gz postgresql-499c81d3a3ffef4645b720ace5909f00609bff79.zip |
Prohibit a column from appearing twice in a PRIMARY KEY or UNIQUE
constraint. This case (a) is useless, (b) violates SQL92, and
(c) is certain to cause a failure downstream when we try to create
an index with duplicated column names. So give an appropriate error
message instead of letting the index failure occur. Per report from
Colin Strickland. NOTE: currently, CREATE INDEX fooi ON foo(f1,f1)
still fails with 'cannot insert duplicate key' error. Should we
change that too? What about functional indexes?
-rw-r--r-- | src/backend/parser/analyze.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index fc03173ce3d..9d6dfeb69a7 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.191 2001/06/25 21:11:44 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.192 2001/07/04 17:36:54 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -954,8 +954,8 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) index = makeNode(IndexStmt); - index->unique = TRUE; - index->primary = (constraint->contype == CONSTR_PRIMARY ? TRUE : FALSE); + index->unique = true; + index->primary = (constraint->contype == CONSTR_PRIMARY); if (index->primary) { if (pkey != NULL) @@ -1057,6 +1057,17 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) elog(ERROR, "CREATE TABLE: column \"%s\" named in key does not exist", key->name); + /* Check for PRIMARY KEY(foo, foo) */ + foreach(columns, index->indexParams) + { + iparam = (IndexElem *) lfirst(columns); + if (strcmp(key->name, iparam->name) == 0) + elog(ERROR, "CREATE TABLE: column \"%s\" appears twice in %s constraint", + key->name, + index->primary ? "PRIMARY KEY" : "UNIQUE"); + } + + /* OK, add it to the index definition */ iparam = makeNode(IndexElem); iparam->name = pstrdup(key->name); iparam->args = NIL; |