diff --git a/mysql-test/suite/innodb/r/foreign_key.result b/mysql-test/suite/innodb/r/foreign_key.result index 509f13715b8..2ccdfe20d0e 100644 --- a/mysql-test/suite/innodb/r/foreign_key.result +++ b/mysql-test/suite/innodb/r/foreign_key.result @@ -897,6 +897,29 @@ create or replace table t1 (a varchar(4096) unique) engine=innodb; create or replace table t2 (pk int primary key, a varchar(4096) unique, foreign key(a) references t1(a) on update cascade) engine=innodb; ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") drop table t1; +# +# MDEV-26824 Can't add foreign key with empty referenced columns list +# +create table t2(a int primary key) engine=innodb; +create table t1(a int primary key, b int) engine=innodb; +alter table t2 add foreign key(a) references t1(a, b); +ERROR 42000: Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match +create or replace table t1(a tinyint primary key) engine innodb; +alter table t2 add foreign key(a) references t1; +ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") +create or replace table t1(b int primary key) engine innodb; +alter table t2 add foreign key(a) references t1; +ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") +create or replace table t1(a int primary key, b int) engine innodb; +alter table t2 add foreign key(a) references t1; +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` int(11) NOT NULL, + PRIMARY KEY (`a`), + CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`a`) REFERENCES `t1` (`a`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +drop tables t2, t1; # End of 10.5 tests # # MDEV-26554 Table-rebuilding DDL on parent table causes crash diff --git a/mysql-test/suite/innodb/t/foreign_key.test b/mysql-test/suite/innodb/t/foreign_key.test index 363aa66237b..07601adffd9 100644 --- a/mysql-test/suite/innodb/t/foreign_key.test +++ b/mysql-test/suite/innodb/t/foreign_key.test @@ -901,6 +901,24 @@ create or replace table t2 (pk int primary key, a varchar(4096) unique, foreign drop table t1; +--echo # +--echo # MDEV-26824 Can't add foreign key with empty referenced columns list +--echo # +create table t2(a int primary key) engine=innodb; +create table t1(a int primary key, b int) engine=innodb; +--error ER_WRONG_FK_DEF +alter table t2 add foreign key(a) references t1(a, b); +create or replace table t1(a tinyint primary key) engine innodb; +--error ER_CANT_CREATE_TABLE +alter table t2 add foreign key(a) references t1; +create or replace table t1(b int primary key) engine innodb; +--error ER_CANT_CREATE_TABLE +alter table t2 add foreign key(a) references t1; +create or replace table t1(a int primary key, b int) engine innodb; +alter table t2 add foreign key(a) references t1; +show create table t2; +drop tables t2, t1; + --echo # End of 10.5 tests --echo # diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 2167d2349de..9b3c3f5a26c 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -2825,15 +2825,19 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, Foreign_key *fk_key= (Foreign_key*) key; if (fk_key->validate(alter_info->create_list)) DBUG_RETURN(TRUE); - if (fk_key->ref_columns.elements && - fk_key->ref_columns.elements != fk_key->columns.elements) + if (fk_key->ref_columns.elements) { - my_error(ER_WRONG_FK_DEF, MYF(0), - (fk_key->name.str ? fk_key->name.str : - "foreign key without name"), - ER_THD(thd, ER_KEY_REF_DO_NOT_MATCH_TABLE_REF)); - DBUG_RETURN(TRUE); + if (fk_key->ref_columns.elements != fk_key->columns.elements) + { + my_error(ER_WRONG_FK_DEF, MYF(0), + (fk_key->name.str ? fk_key->name.str : + "foreign key without name"), + ER_THD(thd, ER_KEY_REF_DO_NOT_MATCH_TABLE_REF)); + DBUG_RETURN(TRUE); + } } + else + fk_key->ref_columns.append(&fk_key->columns); continue; } (*key_count)++; diff --git a/storage/innobase/buf/buf0buf.cc b/storage/innobase/buf/buf0buf.cc index 11d820be305..93934dd2b5b 100644 --- a/storage/innobase/buf/buf0buf.cc +++ b/storage/innobase/buf/buf0buf.cc @@ -2238,6 +2238,10 @@ void buf_page_free(fil_space_t *space, uint32_t page, mtr_t *mtr) } block->page.lock.x_lock(); +#ifdef BTR_CUR_HASH_ADAPT + if (block->index) + btr_search_drop_page_hash_index(block); +#endif /* BTR_CUR_HASH_ADAPT */ block->page.set_freed(block->page.state()); mtr->memo_push(block, MTR_MEMO_PAGE_X_FIX); }