Bugfix for creating foreign key

This commit fixes crash for adding foreign keys without
specifying the referenced column crashes the backend.
pull/1213/head
Onder Kalaci 2017-02-07 09:30:57 +02:00
parent e6e5f63d9d
commit 95f8382ca2
3 changed files with 20 additions and 0 deletions

View File

@ -1591,6 +1591,19 @@ ErrorIfUnsupportedAlterTableStmt(AlterTableStmt *alterTableStatement)
" on co-located tables.")));
}
/*
* The following logic requires the referenced columns to exists in
* the statement. Otherwise, we cannot apply some of the checks.
*/
if (constraint->pk_attrs == NULL)
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot create foreign key constraint "
"because referenced column list is empty"),
errhint("Add column names to \"REFERENCES\" part of "
"the statement.")));
}
/*
* Referencing column's list length should be equal to referenced columns
* list length.

View File

@ -462,6 +462,10 @@ SELECT create_distributed_table('referencing_table', 'ref_id', 'hash');
(1 row)
-- columns for the referenced table is empty
ALTER TABLE referencing_table ADD CONSTRAINT test_constraint FOREIGN KEY(ref_id) REFERENCES referenced_table ON DELETE CASCADE;
ERROR: cannot create foreign key constraint because referenced column list is empty
HINT: Add column names to "REFERENCES" part of the statement.
-- test foreign constraint creation on non-partition columns
ALTER TABLE referencing_table ADD CONSTRAINT test_constraint FOREIGN KEY(id) REFERENCES referenced_table(id);
ERROR: cannot create foreign key constraint

View File

@ -254,6 +254,9 @@ CREATE TABLE referencing_table(id int, ref_id int);
SELECT create_distributed_table('referenced_table', 'id', 'hash');
SELECT create_distributed_table('referencing_table', 'ref_id', 'hash');
-- columns for the referenced table is empty
ALTER TABLE referencing_table ADD CONSTRAINT test_constraint FOREIGN KEY(ref_id) REFERENCES referenced_table ON DELETE CASCADE;
-- test foreign constraint creation on non-partition columns
ALTER TABLE referencing_table ADD CONSTRAINT test_constraint FOREIGN KEY(id) REFERENCES referenced_table(id);