Skip to content

Commit

Permalink
sql: improve error message while adding column with REFERENCE
Browse files Browse the repository at this point in the history
This will eventually be fixed through cockroachdb#32917

Also added a unittest to check that this fails specifically for
a column needing a backfill.

Release note: None
  • Loading branch information
vivekmenezes committed Mar 13, 2019
1 parent 51179af commit 639e713
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,15 @@ func (n *alterTableNode) startExec(params runParams) error {

case *tree.ForeignKeyConstraintTableDef:
for _, colName := range d.FromCols {
col, _, err := n.tableDesc.FindColumnByName(colName)
col, err := n.tableDesc.FindActiveColumnByName(string(colName))
if err != nil {
if _, dropped, inactiveErr := n.tableDesc.FindColumnByName(colName); inactiveErr == nil && !dropped {
return pgerror.UnimplementedWithIssueError(32917,
"adding a REFERENCES constraint while the column is being added not supported")
}
return err
}

if err := col.CheckCanBeFKRef(); err != nil {
return err
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,30 @@ INSERT INTO rides VALUES (567, 'lagos', 'lagos', 10, 100)

statement ok
ALTER TABLE vehicles DROP COLUMN mycol;

# check that adding a reference on a column still being backfilled fails.
# fix through #32917

statement ok
CREATE TABLE t32917 (a INT PRIMARY KEY)

statement ok
INSERT INTO t32917 VALUES (1), (2), (3)

statement ok
CREATE TABLE t32917_2 (b INT PRIMARY KEY)

statement ok
INSERT INTO t32917_2 VALUES (1), (2), (3)

statement ok
BEGIN

statement ok
ALTER TABLE t32917_2 ADD c INT UNIQUE DEFAULT 4

statement error adding a REFERENCES constraint while the column is being added not supported
ALTER TABLE t32917_2 ADD CONSTRAINT fk_c_a FOREIGN KEY (c) references t32917 (a)

statement ok
ROLLBACK
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/fk
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,7 @@ BEGIN; ALTER TABLE parentid ADD id INT NOT NULL AS (k + 2) STORED; ALTER TABLE c
statement ok
ROLLBACK;

statement error column \"id2\" does not exist
statement error adding a REFERENCES constraint while the column is being added not supported
BEGIN; ALTER TABLE childid ADD id2 INT UNIQUE NOT NULL DEFAULT 0; ALTER TABLE childid ADD CONSTRAINT fk_id FOREIGN KEY (id2) REFERENCES parentid (k);

statement ok
Expand Down

0 comments on commit 639e713

Please sign in to comment.