summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Wieck2000-02-05 00:20:38 +0000
committerJan Wieck2000-02-05 00:20:38 +0000
commitad155605739705297be5429d920320d15c4facbe (patch)
treefc9d2813f88e32b349ec695bd4d1c664cba3ab65
parent582ec175c9a858fb9f332b9d4adedc3f998ddf92 (diff)
Enabling automatic primary key detection for self-referencing
FOREIGN KEY constraint during CREATE TABLE. Tnx to Stephan. Jan
-rw-r--r--src/backend/parser/analyze.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index d44f0df091d..a9dda032cde 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: analyze.c,v 1.135 2000/02/04 18:49:32 wieck Exp $
+ * $Id: analyze.c,v 1.136 2000/02/05 00:20:38 wieck Exp $
*
*-------------------------------------------------------------------------
*/
@@ -967,11 +967,36 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
/*
* If the attribute list for the referenced table was
- * omitted, lookup for the definition of the primary key
+ * omitted, lookup for the definition of the primary key.
+ * If the referenced table is this table, use the definition
+ * we found above, rather than looking to the system
+ * tables.
*
*/
if (fkconstraint->fk_attrs != NIL && fkconstraint->pk_attrs == NIL)
- transformFkeyGetPrimaryKey(fkconstraint);
+ if (strcmp(fkconstraint->pktable_name, stmt->relname) != 0)
+ transformFkeyGetPrimaryKey(fkconstraint);
+ else if (pkey != NULL)
+ {
+ List *pkey_attr = pkey->indexParams;
+ List *attr;
+ IndexElem *ielem;
+ Ident *pkattr;
+
+ foreach (attr, pkey_attr)
+ {
+ ielem = lfirst(attr);
+ pkattr = (Ident *)makeNode(Ident);
+ pkattr->name = pstrdup(ielem->name);
+ pkattr->indirection = NIL;
+ pkattr->isRel = false;
+ fkconstraint->pk_attrs = lappend(fkconstraint->pk_attrs, pkattr);
+ }
+ }
+ else {
+ elog(ERROR, "PRIMARY KEY for referenced table \"%s\" not found",
+ fkconstraint->pktable_name);
+ }
/*
* Build a CREATE CONSTRAINT TRIGGER statement for the CHECK