From ed4e086df2283ef9a0b02d64ee8083398710924d Mon Sep 17 00:00:00 2001 From: Marius Posta Date: Mon, 4 Oct 2021 16:13:13 -0400 Subject: [PATCH] sql: fix FK check bug in ALTER COLUMN TYPE This commit fixes an implementation bug when checking that the table's foreign keys didn't hold a reference to the altered column. Fixes #71089. Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE statements to fail when they shouldn't have. --- pkg/sql/alter_column_type.go | 15 ++++++++++++--- .../testdata/logic_test/alter_column_type | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/sql/alter_column_type.go b/pkg/sql/alter_column_type.go index c3a149a08622..b4ce7e2a5b04 100644 --- a/pkg/sql/alter_column_type.go +++ b/pkg/sql/alter_column_type.go @@ -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 + } } } } diff --git a/pkg/sql/logictest/testdata/logic_test/alter_column_type b/pkg/sql/logictest/testdata/logic_test/alter_column_type index f3800069fa66..e7beb627f652 100644 --- a/pkg/sql/logictest/testdata/logic_test/alter_column_type +++ b/pkg/sql/logictest/testdata/logic_test/alter_column_type @@ -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;