diff --git a/mysql-test/suite/innodb/r/foreign_key.result b/mysql-test/suite/innodb/r/foreign_key.result index 68a54344519..f87aeb56b77 100644 --- a/mysql-test/suite/innodb/r/foreign_key.result +++ b/mysql-test/suite/innodb/r/foreign_key.result @@ -892,4 +892,19 @@ DROP INDEX fx ON t1; INSERT INTO t1 VALUES (2,11,11); DROP TABLE t1; SET FOREIGN_KEY_CHECKS=DEFAULT; +# +# MDEV-32018 Allow the setting of Auto_increment on FK referenced columns +# +CREATE TABLE t1 ( +id int unsigned NOT NULL PRIMARY KEY +); +CREATE TABLE t2 ( +id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, +t1_id int unsigned DEFAULT NULL, +CONSTRAINT FK_t1_id FOREIGN KEY (t1_id) REFERENCES t1 (id) +); +ALTER TABLE t1 MODIFY id INT unsigned AUTO_INCREMENT; +DROP TABLE t1,t2; +# # End of 10.4 tests +# diff --git a/mysql-test/suite/innodb/t/foreign_key.test b/mysql-test/suite/innodb/t/foreign_key.test index c4b2b8331b0..032bab2408d 100644 --- a/mysql-test/suite/innodb/t/foreign_key.test +++ b/mysql-test/suite/innodb/t/foreign_key.test @@ -935,6 +935,26 @@ INSERT INTO t1 VALUES (2,11,11); DROP TABLE t1; SET FOREIGN_KEY_CHECKS=DEFAULT; --- echo # End of 10.4 tests +--echo # +--echo # MDEV-32018 Allow the setting of Auto_increment on FK referenced columns +--echo # + +CREATE TABLE t1 ( + id int unsigned NOT NULL PRIMARY KEY +); + +CREATE TABLE t2 ( + id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, + t1_id int unsigned DEFAULT NULL, + CONSTRAINT FK_t1_id FOREIGN KEY (t1_id) REFERENCES t1 (id) +); + +ALTER TABLE t1 MODIFY id INT unsigned AUTO_INCREMENT; + +DROP TABLE t1,t2; + +--echo # +--echo # End of 10.4 tests +--echo # --source include/wait_until_count_sessions.inc diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 987c66e8aeb..6ffdea3b8cd 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -9135,8 +9135,7 @@ enum fk_column_change_type @retval FK_COLUMN_NO_CHANGE No significant changes are to be done on foreign key columns. @retval FK_COLUMN_DATA_CHANGE ALTER TABLE might result in value - change in foreign key column (and - foreign_key_checks is on). + change in foreign key column. @retval FK_COLUMN_RENAMED Foreign key column is renamed. @retval FK_COLUMN_DROPPED Foreign key column is dropped. */ @@ -9172,7 +9171,18 @@ fk_check_column_changes(THD *thd, Alter_info *alter_info, return FK_COLUMN_RENAMED; } - if ((old_field->is_equal(*new_field) == IS_EQUAL_NO) || + /* + Field_{num|decimal}::is_equal evaluates to IS_EQUAL_NO where + the new_field adds an AUTO_INCREMENT flag on a column due to a + limitation in MyISAM/ARIA. For the purposes of FK determination + it doesn't matter if AUTO_INCREMENT is there or not. + */ + const uint flags= new_field->flags; + new_field->flags&= ~AUTO_INCREMENT_FLAG; + const bool equal_result= old_field->is_equal(*new_field); + new_field->flags= flags; + + if ((equal_result == IS_EQUAL_NO) || ((new_field->flags & NOT_NULL_FLAG) && !(old_field->flags & NOT_NULL_FLAG))) {