Skip to content

Commit

Permalink
sql: fix FK check bug in ALTER COLUMN TYPE
Browse files Browse the repository at this point in the history
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes cockroachdb#71089.

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
  • Loading branch information
Marius Posta committed Oct 4, 2021
1 parent d7d6a17 commit ed4e086
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/sql/alter_column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,18 @@ func alterColumnTypeGeneral(
// Disallow ALTER COLUMN TYPE general for columns that have a foreign key
// constraint.
for _, fk := range tableDesc.AllActiveAndInactiveForeignKeys() {
for _, id := range append(fk.OriginColumnIDs, fk.ReferencedColumnIDs...) {
if col.GetID() == id {
return colWithConstraintNotSupportedErr
if fk.OriginTableID == tableDesc.GetID() {
for _, id := range fk.OriginColumnIDs {
if col.GetID() == id {
return colWithConstraintNotSupportedErr
}
}
}
if fk.ReferencedTableID == tableDesc.GetID() {
for _, id := range fk.ReferencedColumnIDs {
if col.GetID() == id {
return colWithConstraintNotSupportedErr
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_column_type
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,13 @@ INSERT INTO regression_54844 VALUES (-9223372036854775807)

statement error integer out of range for type int2
ALTER TABLE regression_54844 ALTER COLUMN i TYPE int2

# Regression test for issue #71089
subtest regression_71089

statement ok
CREATE TABLE parent_71089 (id INT PRIMARY KEY);
CREATE TABLE child_71089 (a INT, b INT REFERENCES parent_71089 (id) NOT NULL)

statement ok
ALTER TABLE child_71089 ALTER COLUMN a TYPE FLOAT;

0 comments on commit ed4e086

Please sign in to comment.