88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.126 2001/05/07 00:43:17 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.127 2001/05/09 21:10:38 momjian Exp $
1212 *
1313 * NOTES
1414 * The PerformAddAttribute() code, like most of the relation
3939#include "parser/parse_expr.h"
4040#include "parser/parse_clause.h"
4141#include "parser/parse_relation.h"
42+ #include "parser/parse_oper.h"
4243#include "nodes/makefuncs.h"
4344#include "optimizer/planmain.h"
4445#include "optimizer/clauses.h"
@@ -1342,6 +1343,13 @@ AlterTableAddConstraint(char *relationName,
13421343 int i ;
13431344 bool found = false;
13441345
1346+ Oid fktypoid [INDEX_MAX_KEYS ];
1347+ Oid pktypoid [INDEX_MAX_KEYS ];
1348+ int attloc ;
1349+
1350+ for (i = 0 ; i < INDEX_MAX_KEYS ; i ++ )
1351+ fktypoid [i ]= pktypoid [i ]= 0 ;
1352+
13451353 if (is_temp_rel_name (fkconstraint -> pktable_name ) &&
13461354 !is_temp_rel_name (relationName ))
13471355 elog (ERROR , "ALTER TABLE / ADD CONSTRAINT: Unable to reference temporary table from permanent table constraint." );
@@ -1403,6 +1411,7 @@ AlterTableAddConstraint(char *relationName,
14031411 found = false;
14041412 else
14051413 {
1414+ attloc = 0 ;
14061415 /* go through the fkconstraint->pk_attrs list */
14071416 foreach (attrl , fkconstraint -> pk_attrs )
14081417 {
@@ -1419,6 +1428,11 @@ AlterTableAddConstraint(char *relationName,
14191428
14201429 if (strcmp (name , attr -> name ) == 0 )
14211430 {
1431+ /* We get the type of this attribute here and
1432+ * store it so we can use it later for making
1433+ * sure the types are comparable.
1434+ */
1435+ pktypoid [attloc ++ ]= rel_attrs [pkattno - 1 ]-> atttypid ;
14221436 found = true;
14231437 break ;
14241438 }
@@ -1448,6 +1462,7 @@ AlterTableAddConstraint(char *relationName,
14481462 Ident * fkattr ;
14491463
14501464 found = false;
1465+ attloc = 0 ;
14511466 foreach (fkattrs , fkconstraint -> fk_attrs )
14521467 {
14531468 int count ;
@@ -1460,6 +1475,11 @@ AlterTableAddConstraint(char *relationName,
14601475
14611476 if (strcmp (name , fkattr -> name ) == 0 )
14621477 {
1478+ /*
1479+ * Here once again we get the types, this
1480+ * time for the fk table's attributes
1481+ */
1482+ fktypoid [attloc ++ ]= rel -> rd_att -> attrs [count ]-> atttypid ;
14631483 found = true;
14641484 break ;
14651485 }
@@ -1471,6 +1491,17 @@ AlterTableAddConstraint(char *relationName,
14711491 elog (ERROR , "columns referenced in foreign key constraint not found." );
14721492 }
14731493
1494+ for (i = 0 ; i < INDEX_MAX_KEYS && fktypoid [i ] != 0 ; i ++ ) {
1495+ /*
1496+ * fktypoid[i] is the foreign key table's i'th element's type oid
1497+ * pktypoid[i] is the primary key table's i'th element's type oid
1498+ * We let oper() do our work for us, including elog(ERROR) if the
1499+ * types can't compare with =
1500+ */
1501+ Operator o = oper ("=" , fktypoid [i ], pktypoid [i ], false);
1502+ ReleaseSysCache (o );
1503+ }
1504+
14741505 trig .tgoid = 0 ;
14751506 if (fkconstraint -> constr_name )
14761507 trig .tgname = fkconstraint -> constr_name ;
0 commit comments