From d1fca0baab272bd4d24b22dd6fa565703eee03ca Mon Sep 17 00:00:00 2001 From: Thirunarayanan Balathandayuthapani Date: Fri, 1 Sep 2023 17:42:47 +0530 Subject: [PATCH] MDEV-32060 Server aborts when table doesn't have referenced index - Server aborts when table doesn't have referenced index. This is caused by 5f09b53bdb4e973e7c7ec2c53a24c98321223f98 (MDEV-31086). While iterating the foreign key constraints, we fail to consider that InnoDB doesn't have referenced index for it when foreign key check is disabled. --- mysql-test/suite/innodb/r/fk_col_alter.result | 13 +++++++++++++ mysql-test/suite/innodb/t/fk_col_alter.test | 15 +++++++++++++++ storage/innobase/handler/handler0alter.cc | 2 ++ 3 files changed, 30 insertions(+) diff --git a/mysql-test/suite/innodb/r/fk_col_alter.result b/mysql-test/suite/innodb/r/fk_col_alter.result index a6888943523..a220402db94 100644 --- a/mysql-test/suite/innodb/r/fk_col_alter.result +++ b/mysql-test/suite/innodb/r/fk_col_alter.result @@ -81,4 +81,17 @@ DROP TABLE t2, t1; CREATE TABLE t (a VARCHAR(40), b INT, C INT) ENGINE=InnoDB; ALTER TABLE t MODIFY a VARCHAR(50), DROP b; DROP TABLE t; +# +# MDEV-32060 Server aborts when table doesn't +# have referenced index +# +SET SESSION FOREIGN_KEY_CHECKS = OFF; +CREATE TABLE t1 (a VARCHAR(16) KEY, FOREIGN KEY(a) REFERENCES t2(b)) ENGINE=InnoDB; +CREATE TABLE t2 (b VARCHAR(8)) ENGINE=InnoDB; +ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") +SET SESSION FOREIGN_KEY_CHECKS = ON; +ALTER TABLE t2 MODIFY b VARCHAR(16), ADD KEY(b); +ERROR 42S02: Table 'test.t2' doesn't exist +DROP TABLE t2, t1; +ERROR 42S02: Unknown table 'test.t2' # End of 10.4 tests diff --git a/mysql-test/suite/innodb/t/fk_col_alter.test b/mysql-test/suite/innodb/t/fk_col_alter.test index 169210e12c1..87ccde5652a 100644 --- a/mysql-test/suite/innodb/t/fk_col_alter.test +++ b/mysql-test/suite/innodb/t/fk_col_alter.test @@ -111,4 +111,19 @@ DROP TABLE t2, t1; CREATE TABLE t (a VARCHAR(40), b INT, C INT) ENGINE=InnoDB; ALTER TABLE t MODIFY a VARCHAR(50), DROP b; DROP TABLE t; + +--echo # +--echo # MDEV-32060 Server aborts when table doesn't +--echo # have referenced index +--echo # +SET SESSION FOREIGN_KEY_CHECKS = OFF; +CREATE TABLE t1 (a VARCHAR(16) KEY, FOREIGN KEY(a) REFERENCES t2(b)) ENGINE=InnoDB; +# Following SQL is allowed in 10.6 +--error ER_CANT_CREATE_TABLE +CREATE TABLE t2 (b VARCHAR(8)) ENGINE=InnoDB; +SET SESSION FOREIGN_KEY_CHECKS = ON; +--error ER_NO_SUCH_TABLE +ALTER TABLE t2 MODIFY b VARCHAR(16), ADD KEY(b); +--error ER_BAD_TABLE_ERROR +DROP TABLE t2, t1; --echo # End of 10.4 tests diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index a5ccb1957f3..bde3c1fb05e 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -7712,6 +7712,8 @@ next_item: { for (ulint i= 0; i < f->n_fields; i++) { + if (!f->referenced_index) + continue; if (f->referenced_index->fields[i].col == col) { my_error(ER_FK_COLUMN_CANNOT_CHANGE_CHILD, MYF(0),