From 321d50fd06c7ddcc9a32ed661c3a91e48178e6f8 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 14 May 2004 12:49:18 +0200 Subject: [PATCH] better fix for bug#3749 - do not consider already removed keys in key removal process mysql-test/r/innodb.result: tests for bug#3749 mysql-test/t/innodb.test: tests for bug#3749 --- mysql-test/r/innodb.result | 25 ++++++++++++++++++++++++- mysql-test/t/innodb.test | 12 +++++++++++- sql/sql_table.cc | 19 ++++++------------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/mysql-test/r/innodb.result b/mysql-test/r/innodb.result index 60280715911..259e1fb3059 100644 --- a/mysql-test/r/innodb.result +++ b/mysql-test/r/innodb.result @@ -1540,4 +1540,27 @@ t2 CREATE TABLE `t2` ( drop table t2; create table t2 (id int(11) not null, id2 int(11) not null, constraint t1_id_fk foreign key (id2,id) references t1 (id)) engine = innodb; ERROR HY000: Can't create table './test/t2.frm' (errno: 150) -drop table t1; +create table t2 (a int auto_increment primary key, b int, index(b), foreign key (b) references t1(id), unique(b)) engine=innodb; +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` int(11) NOT NULL auto_increment, + `b` int(11) default NULL, + PRIMARY KEY (`a`), + UNIQUE KEY `b_2` (`b`), + KEY `b` (`b`), + CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`b`) REFERENCES `t1` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +drop table t2; +create table t2 (a int auto_increment primary key, b int, foreign key (b) references t1(id), foreign key (b) references t1(id), unique(b)) engine=innodb; +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` int(11) NOT NULL auto_increment, + `b` int(11) default NULL, + PRIMARY KEY (`a`), + UNIQUE KEY `b` (`b`), + CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`b`) REFERENCES `t1` (`id`), + CONSTRAINT `t2_ibfk_2` FOREIGN KEY (`b`) REFERENCES `t1` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +drop table t2, t1; diff --git a/mysql-test/t/innodb.test b/mysql-test/t/innodb.test index a260ab1263d..babfdcb535f 100644 --- a/mysql-test/t/innodb.test +++ b/mysql-test/t/innodb.test @@ -1086,4 +1086,14 @@ drop table t2; --error 1005 create table t2 (id int(11) not null, id2 int(11) not null, constraint t1_id_fk foreign key (id2,id) references t1 (id)) engine = innodb; -drop table t1; +# bug#3749 + +create table t2 (a int auto_increment primary key, b int, index(b), foreign key (b) references t1(id), unique(b)) engine=innodb; +show create table t2; +drop table t2; +create table t2 (a int auto_increment primary key, b int, foreign key (b) references t1(id), foreign key (b) references t1(id), unique(b)) engine=innodb; +show create table t2; +drop table t2, t1; + + + diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 47574eab666..8d5ec56add9 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -688,9 +688,10 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info, 'generated', and a generated key is a prefix of the other key. Then we do not need the generated shorter key. */ - if ((key2->type != Key::FOREIGN_KEY && !foreign_key_prefix(key, key2))) + if (key2->name != ignore_key && key2->type != Key::FOREIGN_KEY && + !foreign_key_prefix(key, key2)) { - /* TO DO: issue warning message */ + /* TODO: issue warning message */ /* mark that the generated key should be ignored */ if (!key2->generated || (key->generated && key->columns.elements < @@ -698,17 +699,9 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info, key->name= ignore_key; else { - /* - Remove the previous, generated key if it has not yet been - removed. Note that if we have several identical generated keys, - the last one will remain and others get removed here. - */ - if (key2->name != ignore_key) - { - key2->name= ignore_key; - key_parts-= key2->columns.elements; - (*key_count)--; - } + key2->name= ignore_key; + key_parts-= key2->columns.elements; + (*key_count)--; } break; }