Skip to content

Commit 90c6394

Browse files
committed
This patch should allow primary/foreign key
definitions using inherited columns in the create table statement. Stephan Szabo
1 parent af17128 commit 90c6394

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

src/backend/parser/analyze.c

+64-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: analyze.c,v 1.161 2000/10/07 00:58:17 tgl Exp $
9+
* $Id: analyze.c,v 1.162 2000/11/04 18:29:09 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -965,6 +965,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
965965

966966
foreach(keys, constraint->keys)
967967
{
968+
int found=0;
968969
key = (Ident *) lfirst(keys);
969970
Assert(IsA(key, Ident));
970971
column = NULL;
@@ -975,14 +976,44 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
975976
if (strcmp(column->colname, key->name) == 0)
976977
break;
977978
}
978-
if (columns == NIL) /* fell off end of list? */
979+
if (columns == NIL) { /* try inherited tables */
980+
List *inher;
981+
List *inhRelnames=stmt->inhRelnames;
982+
Relation rel;
983+
foreach (inher, inhRelnames) {
984+
int count=0;
985+
Value *inh=lfirst(inher);
986+
if (inh->type!=T_String) {
987+
elog(ERROR, "inherited table name list returns a non-string");
988+
}
989+
rel=heap_openr(inh->val.str, AccessShareLock);
990+
if (rel->rd_rel->relkind != RELKIND_RELATION)
991+
elog(ERROR, "inherited table \"%s\" is not a relation",
992+
inh->val.str);
993+
for (; count<rel->rd_att->natts; count++) {
994+
char *name=NameStr(rel->rd_att->attrs[count]->attname);
995+
if (strcmp(key->name, name) == 0) {
996+
found=1;
997+
break;
998+
}
999+
}
1000+
heap_close(rel, NoLock);
1001+
if (found)
1002+
break;
1003+
}
1004+
}
1005+
else {
1006+
found=1;
1007+
}
1008+
1009+
if (!found)
9791010
elog(ERROR, "CREATE TABLE: column '%s' named in key does not exist",
9801011
key->name);
9811012

9821013
if (constraint->contype == CONSTR_PRIMARY)
9831014
column->is_not_null = TRUE;
9841015
iparam = makeNode(IndexElem);
985-
iparam->name = pstrdup(column->colname);
1016+
iparam->name = pstrdup(key->name);
9861017
iparam->args = NIL;
9871018
iparam->class = NULL;
9881019
index->indexParams = lappend(index->indexParams, iparam);
@@ -1112,8 +1143,37 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
11121143
if (!found)
11131144
break;
11141145
}
1146+
if (!found) { /* try inherited tables */
1147+
List *inher;
1148+
List *inhRelnames=stmt->inhRelnames;
1149+
Relation rel;
1150+
foreach (inher, inhRelnames) {
1151+
int count=0;
1152+
Value *inh=lfirst(inher);
1153+
if (inh->type!=T_String) {
1154+
elog(ERROR, "inherited table name list returns a non-string");
1155+
}
1156+
rel=heap_openr(inh->val.str, AccessShareLock);
1157+
if (rel->rd_rel->relkind != RELKIND_RELATION)
1158+
elog(ERROR, "inherited table \"%s\" is not a relation",
1159+
inh->val.str);
1160+
for (; count<rel->rd_att->natts; count++) {
1161+
char *name=NameStr(rel->rd_att->attrs[count]->attname);
1162+
if (strcmp(fkattr->name, name) == 0) {
1163+
found=1;
1164+
break;
1165+
}
1166+
}
1167+
heap_close(rel, NoLock);
1168+
if (found)
1169+
break;
1170+
}
1171+
}
1172+
else {
1173+
found=1;
1174+
}
11151175
if (!found)
1116-
elog(ERROR, "columns referenced in foreign key constraint not found.");
1176+
elog(ERROR, "columns in foreign key table of constraint not found.");
11171177
}
11181178

11191179
/*

0 commit comments

Comments
 (0)