6
6
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
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 $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -965,6 +965,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
965
965
966
966
foreach (keys , constraint -> keys )
967
967
{
968
+ int found = 0 ;
968
969
key = (Ident * ) lfirst (keys );
969
970
Assert (IsA (key , Ident ));
970
971
column = NULL ;
@@ -975,14 +976,44 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
975
976
if (strcmp (column -> colname , key -> name ) == 0 )
976
977
break ;
977
978
}
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 )
979
1010
elog (ERROR , "CREATE TABLE: column '%s' named in key does not exist" ,
980
1011
key -> name );
981
1012
982
1013
if (constraint -> contype == CONSTR_PRIMARY )
983
1014
column -> is_not_null = TRUE;
984
1015
iparam = makeNode (IndexElem );
985
- iparam -> name = pstrdup (column -> colname );
1016
+ iparam -> name = pstrdup (key -> name );
986
1017
iparam -> args = NIL ;
987
1018
iparam -> class = NULL ;
988
1019
index -> indexParams = lappend (index -> indexParams , iparam );
@@ -1112,8 +1143,37 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
1112
1143
if (!found )
1113
1144
break ;
1114
1145
}
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
+ }
1115
1175
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." );
1117
1177
}
1118
1178
1119
1179
/*
0 commit comments