|
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.135 2000/02/04 18:49:32 wieck Exp $ |
| 9 | + * $Id: analyze.c,v 1.136 2000/02/05 00:20:38 wieck Exp $ |
10 | 10 | *
|
11 | 11 | *-------------------------------------------------------------------------
|
12 | 12 | */
|
@@ -967,11 +967,36 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
|
967 | 967 |
|
968 | 968 | /*
|
969 | 969 | * If the attribute list for the referenced table was
|
970 |
| - * omitted, lookup for the definition of the primary key |
| 970 | + * omitted, lookup for the definition of the primary key. |
| 971 | + * If the referenced table is this table, use the definition |
| 972 | + * we found above, rather than looking to the system |
| 973 | + * tables. |
971 | 974 | *
|
972 | 975 | */
|
973 | 976 | if (fkconstraint->fk_attrs != NIL && fkconstraint->pk_attrs == NIL)
|
974 |
| - transformFkeyGetPrimaryKey(fkconstraint); |
| 977 | + if (strcmp(fkconstraint->pktable_name, stmt->relname) != 0) |
| 978 | + transformFkeyGetPrimaryKey(fkconstraint); |
| 979 | + else if (pkey != NULL) |
| 980 | + { |
| 981 | + List *pkey_attr = pkey->indexParams; |
| 982 | + List *attr; |
| 983 | + IndexElem *ielem; |
| 984 | + Ident *pkattr; |
| 985 | + |
| 986 | + foreach (attr, pkey_attr) |
| 987 | + { |
| 988 | + ielem = lfirst(attr); |
| 989 | + pkattr = (Ident *)makeNode(Ident); |
| 990 | + pkattr->name = pstrdup(ielem->name); |
| 991 | + pkattr->indirection = NIL; |
| 992 | + pkattr->isRel = false; |
| 993 | + fkconstraint->pk_attrs = lappend(fkconstraint->pk_attrs, pkattr); |
| 994 | + } |
| 995 | + } |
| 996 | + else { |
| 997 | + elog(ERROR, "PRIMARY KEY for referenced table \"%s\" not found", |
| 998 | + fkconstraint->pktable_name); |
| 999 | + } |
975 | 1000 |
|
976 | 1001 | /*
|
977 | 1002 | * Build a CREATE CONSTRAINT TRIGGER statement for the CHECK
|
|
0 commit comments