From: Jan Wieck <JanWieck@Yahoo.com> Date: Sat, 5 Feb 2000 00:20:38 +0000 (+0000) Subject: Enabling automatic primary key detection for self-referencing X-Git-Tag: REL7_0~696 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ad155605739705297be5429d920320d15c4facbe;p=postgresql Enabling automatic primary key detection for self-referencing FOREIGN KEY constraint during CREATE TABLE. Tnx to Stephan. Jan --- diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index d44f0df091..a9dda032cd 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