Skip to content

Commit b4de67d

Browse files
MDEV-32638 MariaDB crashes with foreign_key_checks=0 when changing a column and adding a foreign key at the same time
Problem: ======= - InnoDB fails to find the foreign key index for the newly added foreign key relation. This is caused by commit 5f09b53 (MDEV-31086). FIX: === In check_col_is_in_fk_indexes(), while iterating through the newly added foreign key relationship, InnoDB should consider that foreign key relation may not have foreign index when foreign key check is disabled.
1 parent f9d2fd1 commit b4de67d

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

mysql-test/suite/innodb/r/fk_col_alter.result

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,17 @@ ALTER TABLE t2 DROP INDEX idx;
118118
ALTER TABLE t2 MODIFY f2 VARCHAR(1023);
119119
SET SESSION FOREIGN_KEY_CHECKS = ON;
120120
DROP TABLE t2, t1;
121+
#
122+
# MDEV-32638 MariaDB crashes with foreign_key_checks=0
123+
# when changing a column and adding a foreign
124+
# key at the same time
125+
#
126+
CREATE TABLE t1(f1 VARCHAR(2) NOT NULL, PRIMARY KEY(f1))ENGINE=InnoDB;
127+
CREATE TABLE t2(f1 INT NOT NULL PRIMARY KEY,
128+
f2 VARCHAR(10) NOT NULL DEFAULT '')ENGINE=InnoDB;
129+
SET SESSION FOREIGN_KEY_CHECKS = OFF;
130+
ALTER TABLE t2 CHANGE COLUMN f2 f3 VARCHAR(20) NOT NULL,
131+
ADD CONSTRAINT t2_fk FOREIGN KEY(f3) REFERENCES t1(f1);
132+
DROP TABLE t2, t1;
133+
SET SESSION FOREIGN_KEY_CHECKS = ON;
121134
# End of 10.4 tests

mysql-test/suite/innodb/t/fk_col_alter.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,18 @@ ALTER TABLE t2 DROP INDEX idx;
153153
ALTER TABLE t2 MODIFY f2 VARCHAR(1023);
154154
SET SESSION FOREIGN_KEY_CHECKS = ON;
155155
DROP TABLE t2, t1;
156+
157+
--echo #
158+
--echo # MDEV-32638 MariaDB crashes with foreign_key_checks=0
159+
--echo # when changing a column and adding a foreign
160+
--echo # key at the same time
161+
--echo #
162+
CREATE TABLE t1(f1 VARCHAR(2) NOT NULL, PRIMARY KEY(f1))ENGINE=InnoDB;
163+
CREATE TABLE t2(f1 INT NOT NULL PRIMARY KEY,
164+
f2 VARCHAR(10) NOT NULL DEFAULT '')ENGINE=InnoDB;
165+
SET SESSION FOREIGN_KEY_CHECKS = OFF;
166+
ALTER TABLE t2 CHANGE COLUMN f2 f3 VARCHAR(20) NOT NULL,
167+
ADD CONSTRAINT t2_fk FOREIGN KEY(f3) REFERENCES t1(f1);
168+
DROP TABLE t2, t1;
169+
SET SESSION FOREIGN_KEY_CHECKS = ON;
156170
--echo # End of 10.4 tests

storage/innobase/handler/handler0alter.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7695,6 +7695,7 @@ bool check_col_is_in_fk_indexes(
76957695

76967696
for (const auto &a : add_fk)
76977697
{
7698+
if (!a->foreign_index) continue;
76987699
for (ulint i= 0; i < a->n_fields; i++)
76997700
{
77007701
if (a->foreign_index->fields[i].col == col)

0 commit comments

Comments
 (0)